2
0

MtxRe.Mod 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. (* CAPO - Computational Analysis Platform for Oberon - by Alan Freed and Felix Friedrich. *)
  2. (* Version 1, Update 2 *)
  3. MODULE MtxRe; (** AUTHOR "fof"; PURPOSE "Matrix objects of type Real."; *)
  4. (** caution:
  5. since common indexing order in linear algebra
  6. is row then column, the x coordinate corresponds to row
  7. and y coordinate corresponds to column in this module.
  8. Do not assume x to be horizontal and y to be vertical for
  9. matrices when you use it for linear algebraic computation.
  10. *)
  11. IMPORT SYSTEM, NbrInt, ArrayXdBytes, ArrayXd := ArrayXdRe, Array1d := Array1dRe, DataErrors, Vec := VecRe, NbrRat, MtxInt, MtxRat, DataIO, NbrRe;
  12. CONST
  13. (** The version number used when reading/writing a matrix to file. *)
  14. VERSION* = 1;
  15. TYPE
  16. Value* = ArrayXd.Value; Index* = LONGINT; IntValue = ArrayXd.IntValue; RatValue = NbrRat.Rational;
  17. Array* = ArrayXd.Array2; Map* = ArrayXd.Map;
  18. Matrix* = OBJECT (ArrayXd.Array)
  19. VAR lenx-, leny-: LONGINT;
  20. rows-, cols-: LONGINT; (* lenx = nr.Rows, leny = nr.Columns *)
  21. VAR ox-, oy-: LONGINT;
  22. Get-: PROCEDURE {DELEGATE} ( x, y: Index ): Value;
  23. (* override *)
  24. PROCEDURE AlikeX*( ): ArrayXdBytes.Array;
  25. VAR copy: Matrix;
  26. BEGIN
  27. NEW( copy, origin[0], len[0], origin[1], len[1] ); RETURN copy;
  28. END AlikeX;
  29. PROCEDURE NewRangeX*( neworigin, newlen: ArrayXdBytes.IndexArray; copydata: BOOLEAN );
  30. BEGIN
  31. IF LEN( newlen ) # 2 THEN HALT( 1001 ) END;
  32. NewRangeX^( neworigin, newlen, copydata );
  33. END NewRangeX;
  34. PROCEDURE ValidateCache*;
  35. BEGIN
  36. ValidateCache^;
  37. IF dim # 2 THEN HALT( 100 ) END;
  38. lenx := len[0]; leny := len[1]; ox := origin[0]; oy := origin[1]; rows := lenx; cols := leny;
  39. END ValidateCache;
  40. PROCEDURE SetBoundaryCondition*( c: SHORTINT ); (* called by new, load and directly *)
  41. BEGIN
  42. SetBoundaryCondition^( c );
  43. CASE c OF
  44. ArrayXd.StrictBoundaryC:
  45. Get := Get2;
  46. | ArrayXd.AbsorbingBoundaryC:
  47. Get := Get2BAbsorbing;
  48. | ArrayXd.PeriodicBoundaryC:
  49. Get := Get2BPeriodic;
  50. | ArrayXd.SymmetricOnBoundaryC:
  51. Get := Get2BSymmetricOnB
  52. | ArrayXd.SymmetricOffBoundaryC:
  53. Get := Get2BSymmetricOffB
  54. | ArrayXd.AntisymmetricOnBoundaryC:
  55. Get := Get2BAntisymmetricOnB
  56. | ArrayXd.AntisymmetricOffBoundaryC:
  57. Get := Get2BAntisymmetricOffB
  58. END;
  59. END SetBoundaryCondition;
  60. (** new*)
  61. PROCEDURE & New*( ox, rowsORw, oy, colsORh: LONGINT );
  62. BEGIN
  63. NewXdB( ArrayXdBytes.Array2( ox, oy ), ArrayXdBytes.Array2( rowsORw, colsORh ) );
  64. END New;
  65. PROCEDURE Alike*( ): Matrix;
  66. VAR copy: ArrayXdBytes.Array;
  67. BEGIN
  68. copy := AlikeX(); RETURN copy( Matrix );
  69. END Alike;
  70. PROCEDURE NewRange*( ox, rowsORw, oy, colsORh: LONGINT; copydata: BOOLEAN );
  71. BEGIN
  72. IF (rowsORw # len[0]) OR (colsORh # len[1]) OR (ox # origin[0]) OR (oy # origin[1]) THEN
  73. NewRangeX^( ArrayXdBytes.Array2( ox, oy ), ArrayXdBytes.Array2( rowsORw, colsORh ), copydata )
  74. END;
  75. END NewRange;
  76. PROCEDURE Copy*( ): Matrix;
  77. VAR res: ArrayXdBytes.Array;
  78. BEGIN
  79. res := CopyX(); RETURN res( Matrix );
  80. END Copy;
  81. PROCEDURE Set*( rowORx (**= row or x coordinate*) , colORy (**= column or y coordinate *) : Index; v: Value );
  82. BEGIN
  83. ArrayXdBytes.Set2( SELF, rowORx, colORy, v );
  84. END Set;
  85. (** Exchanges values held by mtx[row1, k] and mtx[row2, k], 0 # k < cols. *)
  86. PROCEDURE SwapRows*( row1, row2: Index );
  87. BEGIN
  88. ToggleElements( 0, row1, row2 )
  89. END SwapRows;
  90. (** Exchanges values held by mtx[k, col1] and mtx[k, col2], 0 # k < rows. *)
  91. PROCEDURE SwapColumns*( col1, col2: Index );
  92. BEGIN
  93. ToggleElements( 1, col1, col2 )
  94. END SwapColumns;
  95. PROCEDURE Transpose*;
  96. BEGIN
  97. PermuteDimensions( ArrayXdBytes.Array2( 1, 0 ), TRUE );
  98. END Transpose;
  99. (** Stores mtx := mtx * x which requires x to be square and to have dimension cols X cols *)
  100. PROCEDURE Dot*( x: Matrix );
  101. VAR res: Matrix;
  102. BEGIN
  103. IF x # NIL THEN
  104. IF x.len[0] = x.len[1] THEN
  105. IF len[0] # len[1] THEN
  106. IF len[0] = x.len[0] THEN
  107. res := SELF * x; ArrayXdBytes.CopyArrayPartToArrayPart( res, SELF, res.origin, res.len, origin, len );
  108. ELSE DataErrors.Error( "The two matrices were not compatible" )
  109. END;
  110. ELSE DataErrors.Error( "This matrix in not square." )
  111. END
  112. ELSE DataErrors.Error( "The supplied matrix was not square." )
  113. END
  114. ELSE DataErrors.Error( "The supplied matrix to be doted was NIL." )
  115. END
  116. END Dot;
  117. (** Stores mtx := mtxT * x which requires both matrices to be square and have equal dimensions *)
  118. PROCEDURE LeftDot*( x: Matrix );
  119. BEGIN
  120. Transpose; Dot( x );
  121. END LeftDot;
  122. (** Stores mtx := mtx * xT which requires both matrices to be square and have equal dimensions *)
  123. PROCEDURE RightDot*( x: Matrix );
  124. BEGIN
  125. x.Transpose; Dot( x ); x.Transpose;
  126. END RightDot;
  127. PROCEDURE Row*( row: Index ): Vec.Vector;
  128. VAR v: Vec.Vector;
  129. BEGIN
  130. NEW( v, 0, len[1] ); ArrayXd.CopyMtxToVec( SELF, v, 1, row, 0, 0, len[1] ); RETURN v;
  131. END Row;
  132. PROCEDURE InsertRow*( at: Index );
  133. BEGIN
  134. InsertElements( 0, at, 1 ) (* insert elements in each col *)
  135. END InsertRow;
  136. PROCEDURE DeleteRow*( x: Index );
  137. BEGIN
  138. DeleteElements( 0, x, 1 ); (* delete elements from each col *)
  139. END DeleteRow;
  140. PROCEDURE Col*( col: Index ): Vec.Vector;
  141. VAR v: Vec.Vector;
  142. BEGIN
  143. NEW( v, 0, len[0] ); ArrayXd.CopyMtxToVec( SELF, v, 0, 0, col, 0, len[0] ); RETURN v;
  144. END Col;
  145. PROCEDURE InsertCol*( at: Index );
  146. BEGIN
  147. InsertElements( 1, at, 1 )
  148. END InsertCol;
  149. PROCEDURE DeleteCol*( x: Index );
  150. BEGIN
  151. DeleteElements( 1, x, 1 );
  152. END DeleteCol;
  153. (** copy methods using the current boundary condition SELF.bc*)
  154. PROCEDURE CopyToVec*( dest: ArrayXd.Array; dim: Index; srcx, srcy, destx, len: Index );
  155. VAR slen: ArrayXdBytes.IndexArray;
  156. BEGIN
  157. IF (dest.dim # 1) THEN HALT( 1002 ) END;
  158. slen := ArrayXdBytes.Index2( 1, 1 ); slen[dim] := len;
  159. CopyToArray( dest, ArrayXdBytes.Index2( srcx, srcy ), slen, ArrayXdBytes.Index1( destx ), ArrayXdBytes.Index1( len ) );
  160. END CopyToVec;
  161. PROCEDURE CopyToMtx*( dest: ArrayXd.Array; srcx, srcy, destx, desty, lenx, leny: Index );
  162. VAR slen: ArrayXdBytes.IndexArray;
  163. BEGIN
  164. IF (dest.dim # 2) THEN HALT( 1005 ) END;
  165. slen := ArrayXdBytes.Index2( lenx, leny );
  166. CopyToArray( dest, ArrayXdBytes.Index2( srcx, srcy ), slen, ArrayXdBytes.Index2( destx, desty ), slen );
  167. END CopyToMtx;
  168. PROCEDURE CopyToCube*( dest: ArrayXd.Array; dimx, dimy: Index; srcx, srcy, destx, desty, destz, lenx, leny: Index );
  169. VAR slen: ArrayXdBytes.IndexArray;
  170. BEGIN
  171. IF (dest.dim # 3) OR (dimx >= dimy) THEN HALT( 1005 ) END;
  172. slen := ArrayXdBytes.Index3( 1, 1, 1 ); slen[dimx] := lenx; slen[dimy] := leny;
  173. CopyToArray( dest, ArrayXdBytes.Index2( srcx, srcy ), ArrayXdBytes.Index2( lenx, leny ),
  174. ArrayXdBytes.Index3( destx, desty, destz ), slen );
  175. END CopyToCube;
  176. PROCEDURE CopyToHCube*( dest: ArrayXd.Array; dimx, dimy: Index;
  177. srcx, srcy, destx, desty, destz, destt, lenx, leny: Index );
  178. VAR slen: ArrayXdBytes.IndexArray;
  179. BEGIN
  180. IF (dest.dim # 4) OR (dimx >= dimy) THEN HALT( 1005 ) END;
  181. slen := ArrayXdBytes.Index4( 1, 1, 1, 1 ); slen[dimx] := lenx; slen[dimy] := leny;
  182. CopyToArray( dest, ArrayXdBytes.Index2( srcx, srcy ), ArrayXdBytes.Index2( lenx, leny ),
  183. ArrayXdBytes.Index4( destx, desty, destz, destt ), slen );
  184. END CopyToHCube;
  185. PROCEDURE CopyTo1dArray*( VAR dest: ARRAY OF Value; sx, sy, slenx, sleny: Index; dpos, dlen: LONGINT );
  186. VAR destm: ArrayXdBytes.ArrayMemoryStructure;
  187. BEGIN
  188. destm :=
  189. ArrayXdBytes.MakeMemoryStructure( 1, ArrayXdBytes.Index1( 0 ), ArrayXdBytes.Index1( LEN( dest ) ), SIZEOF( Value ),
  190. ADDRESSOF( dest[0] ) );
  191. ArrayXd.CopyArrayToArrayPartB( SELF, destm, bc, ArrayXdBytes.Index2( sx, sy ), ArrayXdBytes.Index2( slenx, sleny ),
  192. ArrayXdBytes.Index1( dpos ), ArrayXdBytes.Index1( dlen ) );
  193. END CopyTo1dArray;
  194. PROCEDURE CopyTo2dArray*( VAR dest: ARRAY OF ARRAY OF Value; sx, sy, slenx, sleny: Index; dposx, dposy, dlenx, dleny: LONGINT );
  195. VAR destm: ArrayXdBytes.ArrayMemoryStructure;
  196. BEGIN
  197. destm :=
  198. ArrayXdBytes.MakeMemoryStructure( 2, ArrayXdBytes.Index2( 0, 0 ), ArrayXdBytes.Index2( LEN( dest, 1 ), LEN( dest, 0 ) ),
  199. SIZEOF( Value ), ADDRESSOF( dest[0, 0] ) );
  200. ArrayXd.CopyArrayToArrayPartB( SELF, destm, bc, ArrayXdBytes.Index2( sx, sy ), ArrayXdBytes.Index2( slenx, sleny ),
  201. ArrayXdBytes.Index2( dposx, dposy ), ArrayXdBytes.Index2( dlenx, dleny ) );
  202. END CopyTo2dArray;
  203. PROCEDURE CopyTo3dArray*( VAR dest: ARRAY OF ARRAY OF ARRAY OF Value; sx, sy, slenx, sleny: Index;
  204. dposx, dposy, dposz, dlenx, dleny, dlenz: LONGINT );
  205. VAR destm: ArrayXdBytes.ArrayMemoryStructure;
  206. BEGIN
  207. destm :=
  208. ArrayXdBytes.MakeMemoryStructure( 3, ArrayXdBytes.Index3( 0, 0, 0 ),
  209. ArrayXdBytes.Index3( LEN( dest, 2 ), LEN( dest, 1 ), LEN( dest, 0 ) ), SIZEOF( Value ),
  210. ADDRESSOF( dest[0, 0, 0] ) );
  211. ArrayXd.CopyArrayToArrayPartB( SELF, destm, bc, ArrayXdBytes.Index2( sx, sy ), ArrayXdBytes.Index2( slenx, sleny ),
  212. ArrayXdBytes.Index3( dposx, dposy, dposz ),
  213. ArrayXdBytes.Index3( dlenx, dleny, dlenz ) );
  214. END CopyTo3dArray;
  215. PROCEDURE CopyTo4dArray*( VAR dest: ARRAY OF ARRAY OF ARRAY OF ARRAY OF Value; sx, sy, slenx, sleny: Index;
  216. dposx, dposy, dposz, dpost, dlenx, dleny, dlenz, dlent: LONGINT );
  217. VAR destm: ArrayXdBytes.ArrayMemoryStructure;
  218. BEGIN
  219. destm :=
  220. ArrayXdBytes.MakeMemoryStructure( 4, ArrayXdBytes.Index4( 0, 0, 0, 0 ),
  221. ArrayXdBytes.Index4( LEN( dest, 3 ), LEN( dest, 2 ), LEN( dest, 1 ), LEN( dest, 0 ) ), SIZEOF( Value ),
  222. ADDRESSOF( dest[0, 0, 0, 0] ) );
  223. ArrayXd.CopyArrayToArrayPartB( SELF, destm, bc, ArrayXdBytes.Index2( sx, sy ), ArrayXdBytes.Index2( slenx, sleny ),
  224. ArrayXdBytes.Index4( dposx, dposy, dposz, dpost ),
  225. ArrayXdBytes.Index4( dlenx, dleny, dlenz, dlent ) );
  226. END CopyTo4dArray;
  227. (** copy from without boundary conditions *)
  228. PROCEDURE CopyFrom1dArray*( VAR src: ARRAY OF Value; spos, slen: Index; dx, dy, dlenx, dleny: Index );
  229. VAR srcm: ArrayXdBytes.ArrayMemoryStructure;
  230. BEGIN
  231. srcm :=
  232. ArrayXdBytes.MakeMemoryStructure( 1, ArrayXdBytes.Index1( 0 ), ArrayXdBytes.Index1( LEN( src ) ), SIZEOF( Value ),
  233. ADDRESSOF( src[0] ) );
  234. ArrayXdBytes.CopyArrayPartToArrayPart( srcm, SELF, ArrayXdBytes.Index1( spos ), ArrayXdBytes.Index1( slen ),
  235. ArrayXdBytes.Index2( dx, dy ), ArrayXdBytes.Index2( dlenx, dleny ) );
  236. END CopyFrom1dArray;
  237. PROCEDURE CopyFrom2dArray*( VAR src: ARRAY OF ARRAY OF Value; sposx, spoxy, slenx, sleny: Index;
  238. dx, dy, dlenx, dleny: Index );
  239. VAR srcm: ArrayXdBytes.ArrayMemoryStructure;
  240. BEGIN
  241. srcm :=
  242. ArrayXdBytes.MakeMemoryStructure( 2, ArrayXdBytes.Index2( 0, 0 ), ArrayXdBytes.Index2( LEN( src, 1 ), LEN( src, 0 ) ),
  243. SIZEOF( Value ), ADDRESSOF( src[0, 0] ) );
  244. ArrayXdBytes.CopyArrayPartToArrayPart( srcm, SELF, ArrayXdBytes.Index2( sposx, spoxy ),
  245. ArrayXdBytes.Index2( slenx, sleny ), ArrayXdBytes.Index2( dx, dy ),
  246. ArrayXdBytes.Index2( dlenx, dleny ) );
  247. END CopyFrom2dArray;
  248. PROCEDURE CopyFrom3dArray*( VAR src: ARRAY OF ARRAY OF ARRAY OF Value; sposx, spoxy, sposz, slenx, sleny, slenz: Index;
  249. dx, dy, dlenx, dleny: Index );
  250. VAR srcm: ArrayXdBytes.ArrayMemoryStructure;
  251. BEGIN
  252. srcm :=
  253. ArrayXdBytes.MakeMemoryStructure( 3, ArrayXdBytes.Index3( 0, 0, 0 ),
  254. ArrayXdBytes.Index3( LEN( src, 2 ), LEN( src, 1 ), LEN( src, 0 ) ), SIZEOF( Value ),
  255. ADDRESSOF( src[0, 0, 0] ) );
  256. ArrayXdBytes.CopyArrayPartToArrayPart( srcm, SELF, ArrayXdBytes.Index3( sposx, spoxy, sposz ),
  257. ArrayXdBytes.Index3( slenx, sleny, slenz ), ArrayXdBytes.Index2( dx, dy ),
  258. ArrayXdBytes.Index2( dlenx, dleny ) );
  259. END CopyFrom3dArray;
  260. PROCEDURE CopyFrom4dArray*( VAR src: ARRAY OF ARRAY OF ARRAY OF ARRAY OF Value;
  261. sposx, spoxy, sposz, spost, slenx, sleny, slenz, slent: Index; dx, dy, dlenx, dleny: Index );
  262. VAR srcm: ArrayXdBytes.ArrayMemoryStructure;
  263. BEGIN
  264. srcm :=
  265. ArrayXdBytes.MakeMemoryStructure( 4, ArrayXdBytes.Index4( 0, 0, 0, 0 ),
  266. ArrayXdBytes.Index4( LEN( src, 3 ), LEN( src, 2 ), LEN( src, 1 ), LEN( src, 0 ) ), SIZEOF( Value ),
  267. ADDRESSOF( src[0, 0, 0, 0] ) );
  268. ArrayXdBytes.CopyArrayPartToArrayPart( srcm, SELF, ArrayXdBytes.Index4( sposx, spoxy, sposz, spost ),
  269. ArrayXdBytes.Index4( slenx, sleny, slenz, slent ),
  270. ArrayXdBytes.Index2( dx, dy ), ArrayXdBytes.Index2( dlenx, dleny ) );
  271. END CopyFrom4dArray;
  272. END Matrix;
  273. PROCEDURE FrobeniusNorm*( m: Matrix ): NbrRe.Real;
  274. BEGIN
  275. RETURN Array1d.L2Norm( m.data^, 0, LEN( m.data ) );
  276. END FrobeniusNorm;
  277. PROCEDURE Transpose*( u: Matrix ): Matrix;
  278. VAR res: Matrix;
  279. BEGIN
  280. res := u.Copy(); res.Transpose; RETURN res;
  281. END Transpose;
  282. OPERATOR ":="*( VAR l: Matrix; VAR r: ARRAY OF ARRAY OF Value );
  283. BEGIN
  284. (* IF r = NIL THEN l := NIL; RETURN END; *)
  285. IF l = NIL THEN NEW( l, 0, LEN( r, 1 ), 0, LEN( r, 0 ) ) ELSE l.NewRange( 0, LEN( r, 1 ), 0, LEN( r, 0 ), FALSE ); END;
  286. ArrayXdBytes.CopyMemoryToArrayPart( ADDRESSOF( r[0, 0] ), l, LEN( r, 1 ) * LEN( r, 0 ), NIL , NIL );
  287. END ":=";
  288. OPERATOR ":="*( VAR l: Matrix; r: Vec.Vector );
  289. BEGIN
  290. (* IF r = NIL THEN l := NIL; RETURN END; *)
  291. IF l = NIL THEN NEW( l, 0, 1, 0, r.len[0] ) ELSE l.NewRange( 0, 1, 0, r.len[0], FALSE ); END;
  292. ArrayXdBytes.CopyDataRaw( r, l );
  293. END ":=";
  294. OPERATOR ":="*( VAR l: Matrix; r: MtxInt.Matrix );
  295. VAR i, last: LONGINT;
  296. BEGIN
  297. IF r = NIL THEN l := NIL ELSE
  298. IF l = NIL THEN NEW( l, r.origin[0], r.len[0], r.origin[1], r.len[1] ); END;
  299. last := LEN( r.data ) - 1;
  300. FOR i := 0 TO last DO l.data[i] := r.data[i]; END;
  301. END;
  302. END ":=";
  303. OPERATOR ":="*( VAR l: Matrix; r: MtxRat.Matrix );
  304. VAR i, last: LONGINT;
  305. BEGIN
  306. IF r = NIL THEN l := NIL ELSE
  307. IF l = NIL THEN NEW( l, r.origin[0], r.len[0], r.origin[1], r.len[1] ); END;
  308. last := LEN( r.data ) - 1;
  309. FOR i := 0 TO last DO l.data[i] := r.data[i]; END;
  310. END;
  311. END ":=";
  312. OPERATOR ":="*( VAR l: Matrix; r: Value );
  313. BEGIN
  314. IF l # NIL THEN ArrayXd.Fill( l, r ); END;
  315. END ":=";
  316. OPERATOR ":="*( VAR l: Matrix; r: RatValue );
  317. VAR r1: Value;
  318. BEGIN
  319. r1 := r; l := r1;
  320. END ":=";
  321. OPERATOR ":="*( VAR l: Matrix; r: IntValue );
  322. VAR r1: Value;
  323. BEGIN
  324. r1 := r; l := r1;
  325. END ":=";
  326. OPERATOR "+"*( l, r: Matrix ): Matrix;
  327. VAR res: Matrix;
  328. BEGIN
  329. res := l.Alike(); ArrayXd.Add( l, r, res ); RETURN res;
  330. END "+";
  331. OPERATOR "-"*( l, r: Matrix ): Matrix;
  332. VAR res: Matrix;
  333. BEGIN
  334. res := l.Alike(); ArrayXd.Sub( l, r, res ); RETURN res;
  335. END "-";
  336. OPERATOR "+"*( l: Matrix; r: Value ): Matrix;
  337. VAR res: Matrix;
  338. BEGIN
  339. res := l.Alike(); ArrayXd.AddAV( l, r, res ); RETURN res;
  340. END "+";
  341. OPERATOR "+"*( l: Matrix; r: RatValue ): Matrix;
  342. VAR res: Matrix; r1: Value;
  343. BEGIN
  344. res := l.Alike(); r1 := r; ArrayXd.AddAV( l, r1, res ); RETURN res;
  345. END "+";
  346. OPERATOR "+"*( l: Matrix; r: IntValue ): Matrix;
  347. VAR res: Matrix; r1: Value;
  348. BEGIN
  349. res := l.Alike(); r1 := r; ArrayXd.AddAV( l, r1, res ); RETURN res;
  350. END "+";
  351. OPERATOR "+"*( l: Value; r: Matrix ): Matrix;
  352. BEGIN
  353. RETURN r + l
  354. END "+";
  355. OPERATOR "+"*( l: RatValue; r: Matrix ): Matrix;
  356. BEGIN
  357. RETURN r + l
  358. END "+";
  359. OPERATOR "+"*( l: IntValue; r: Matrix ): Matrix;
  360. BEGIN
  361. RETURN r + l
  362. END "+";
  363. OPERATOR "-"*( l: Matrix; r: Value ): Matrix;
  364. VAR res: Matrix;
  365. BEGIN
  366. res := l.Alike(); ArrayXd.SubAV( l, r, res ); RETURN res;
  367. END "-";
  368. OPERATOR "-"*( l: Matrix; r: RatValue ): Matrix;
  369. VAR res: Matrix; r1: Value;
  370. BEGIN
  371. res := l.Alike(); r1 := r; ArrayXd.SubAV( l, r1, res ); RETURN res;
  372. END "-";
  373. OPERATOR "-"*( l: Matrix; r: IntValue ): Matrix;
  374. VAR res: Matrix; r1: Value;
  375. BEGIN
  376. res := l.Alike(); r1 := r; ArrayXd.SubAV( l, r1, res ); RETURN res;
  377. END "-";
  378. OPERATOR "-"*( l: Value; r: Matrix ): Matrix;
  379. VAR res: Matrix;
  380. BEGIN
  381. res := r.Alike(); ArrayXd.SubVA( l, r, res ); RETURN res;
  382. END "-";
  383. OPERATOR "-"*( l: RatValue; r: Matrix ): Matrix;
  384. VAR res: Matrix; l1: Value;
  385. BEGIN
  386. res := r.Alike(); l1 := l; ArrayXd.SubVA( l1, r, res ); RETURN res;
  387. END "-";
  388. OPERATOR "-"*( l: IntValue; r: Matrix ): Matrix;
  389. VAR res: Matrix; l1: Value;
  390. BEGIN
  391. res := r.Alike(); l1 := l; ArrayXd.SubVA( l1, r, res ); RETURN res;
  392. END "-";
  393. OPERATOR "-"*( l: Matrix ): Matrix;
  394. BEGIN
  395. RETURN 0 - l;
  396. END "-";
  397. OPERATOR "*"*( l: Matrix; r: Value ): Matrix;
  398. VAR res: Matrix;
  399. BEGIN
  400. res := l.Alike(); ArrayXd.MulAV( l, r, res ); RETURN res;
  401. END "*";
  402. OPERATOR "*"*( l: Matrix; r: RatValue ): Matrix;
  403. VAR res: Matrix; r1: Value;
  404. BEGIN
  405. res := l.Alike(); r1 := r; ArrayXd.MulAV( l, r1, res ); RETURN res;
  406. END "*";
  407. OPERATOR "*"*( l: Matrix; r: IntValue ): Matrix;
  408. VAR res: Matrix; r1: Value;
  409. BEGIN
  410. res := l.Alike(); r1 := r; ArrayXd.MulAV( l, r1, res ); RETURN res;
  411. END "*";
  412. OPERATOR "*"*( l: Value; r: Matrix ): Matrix;
  413. BEGIN
  414. RETURN r * l;
  415. END "*";
  416. OPERATOR "*"*( l: RatValue; r: Matrix ): Matrix;
  417. BEGIN
  418. RETURN r * l;
  419. END "*";
  420. OPERATOR "*"*( l: IntValue; r: Matrix ): Matrix;
  421. BEGIN
  422. RETURN r * l;
  423. END "*";
  424. OPERATOR "/"*( l: Matrix; r: Value ): Matrix;
  425. VAR res: Matrix;
  426. BEGIN
  427. res := l.Alike(); ArrayXd.DivAV( l, r, res ); RETURN res;
  428. END "/";
  429. OPERATOR "/"*( l: Matrix; r: RatValue ): Matrix;
  430. VAR res: Matrix; r1: Value;
  431. BEGIN
  432. res := l.Alike(); r1 := r; ArrayXd.DivAV( l, r1, res ); RETURN res;
  433. END "/";
  434. OPERATOR "/"*( l: Matrix; r: IntValue ): Matrix;
  435. VAR res: Matrix; r1: Value;
  436. BEGIN
  437. res := l.Alike(); r1 := r; ArrayXd.DivAV( l, r1, res ); RETURN res;
  438. END "/";
  439. OPERATOR "/"*( l: Value; r: Matrix ): Matrix;
  440. VAR res: Matrix;
  441. BEGIN
  442. res := r.Alike(); ArrayXd.DivVA( l, r, res ); RETURN res;
  443. END "/";
  444. OPERATOR "/"*( l: RatValue; r: Matrix ): Matrix;
  445. VAR res: Matrix; l1: Value;
  446. BEGIN
  447. res := r.Alike(); l1 := l; ArrayXd.DivVA( l1, r, res ); RETURN res;
  448. END "/";
  449. OPERATOR "/"*( l: IntValue; r: Matrix ): Matrix;
  450. VAR res: Matrix; l1: Value;
  451. BEGIN
  452. res := r.Alike(); l1 := l; ArrayXd.DivVA( l1, r, res ); RETURN res;
  453. END "/";
  454. (*
  455. OPERATOR "MOD"*( l: Matrix; r: Value ): Matrix;
  456. VAR res: Matrix;
  457. BEGIN
  458. res := l.Alike(); ArrayXd.ModAV( l, r, res ); RETURN res;
  459. END "MOD";
  460. OPERATOR "MOD"*( l: Value; r: Matrix ): Matrix;
  461. VAR res: Matrix;
  462. BEGIN
  463. res := r.Alike(); ArrayXd.ModVA( l, r, res ); RETURN res;
  464. END "MOD";
  465. *)
  466. OPERATOR "*"*( l: Vec.Vector; r: Matrix ): Vec.Vector;
  467. VAR res: Vec.Vector; rc, rr, lc, i, j: LONGINT; sum: Value;
  468. BEGIN
  469. rc := r.cols; rr := r.rows; lc := l.lenx;
  470. IF lc # rr THEN DataErrors.Error( "The supplied matrix / vector were incompatible." ); HALT( 100 );
  471. ELSE
  472. NEW( res, 0, rc );
  473. FOR i := 0 TO rc - 1 DO (* right columns *)
  474. sum := 0;
  475. FOR j := 0 TO lc - 1 DO sum := sum + l.Get( j ) * r.Get( j, i ); END;
  476. res.Set( i, sum );
  477. END;
  478. END;
  479. RETURN res;
  480. END "*";
  481. OPERATOR "*"*( l: Matrix; r: Vec.Vector ): Vec.Vector;
  482. VAR res: Vec.Vector; lr, lc, rr, i, j: LONGINT; sum: Value;
  483. BEGIN
  484. lr := l.rows; lc := l.cols; rr := r.lenx;
  485. IF lc # rr THEN DataErrors.Error( "The supplied matrix / vector were incompatible." ); HALT( 100 );
  486. ELSE
  487. NEW( res, 0, lr );
  488. FOR i := 0 TO lr - 1 DO
  489. sum := 0;
  490. FOR j := 0 TO lc - 1 DO sum := sum + l.Get( i, j ) * r.Get( j ); END;
  491. res.Set( i, sum );
  492. END;
  493. END;
  494. RETURN res;
  495. END "*";
  496. OPERATOR "*"*( l, r: Matrix ): Matrix;
  497. VAR res: Matrix; rr, rc, lr, lc, i, j, k: LONGINT; sum: Value;
  498. (**! far from opimal: use internal routines of ArrayXdBytes *)
  499. BEGIN
  500. rc := r.cols; (* columns of right matrix *)
  501. rr := r.rows; (* rows of right matrix *)
  502. lr := l.rows; lc := l.cols;
  503. IF lc # rr THEN DataErrors.Error( "The supplied matrices were incompatible." ); HALT( 100 )
  504. ELSE
  505. NEW( res, 0, lr, 0, rc );
  506. FOR i := 0 TO lr - 1 DO (* left rows*)
  507. FOR j := 0 TO rc - 1 DO (* right columns *)
  508. sum := 0;
  509. FOR k := 0 TO lc - 1 (* = rr -1 *) DO sum := sum + l.Get( i, k ) * r.Get( k, j ); END;
  510. res.Set( i, j, sum );
  511. END;
  512. END;
  513. END;
  514. RETURN res;
  515. END "*";
  516. (* The procedures needed to register type Matrix so that its instances can be made persistent. *)
  517. PROCEDURE LoadMatrix( R: DataIO.Reader; VAR obj: OBJECT );
  518. VAR a: Matrix; version: SHORTINT; ver: NbrInt.Integer;
  519. BEGIN
  520. R.RawSInt( version );
  521. IF version = -1 THEN
  522. obj := NIL (* Version tag is -1 for NIL. *)
  523. ELSIF version = VERSION THEN NEW( a, 0, 0, 0, 0 ); a.Read( R ); obj := a
  524. ELSE (* Encountered an unknown version number. *)
  525. ver := version; DataErrors.IntError( ver, "Alien version number encountered." ); HALT( 1000 )
  526. END
  527. END LoadMatrix;
  528. PROCEDURE StoreMatrix( W: DataIO.Writer; obj: OBJECT );
  529. VAR a: Matrix;
  530. BEGIN
  531. IF obj = NIL THEN W.RawSInt( -1 ) ELSE W.RawSInt( VERSION ); a := obj( Matrix ); a.Write( W ) END
  532. END StoreMatrix;
  533. PROCEDURE Register;
  534. VAR a: Matrix;
  535. BEGIN
  536. NEW( a, 0, 0, 0, 0 ); DataIO.PlugIn( a, LoadMatrix, StoreMatrix )
  537. END Register;
  538. (** Load and Store are procedures for external use that read/write an instance of Matrix from/to a file. *)
  539. PROCEDURE Load*( R: DataIO.Reader; VAR obj: Matrix );
  540. VAR ptr: OBJECT;
  541. BEGIN
  542. R.Object( ptr ); obj := ptr( Matrix )
  543. END Load;
  544. PROCEDURE Store*( W: DataIO.Writer; obj: Matrix );
  545. BEGIN
  546. W.Object( obj )
  547. END Store;
  548. BEGIN
  549. Register
  550. END MtxRe.