1 /**************************************************/
2 /* Some common operations on matrices. */
3 /**************************************************/
5 /*** Include files ***/
13 #include "../erroridt.h"
14 #include "../Polylib/polytypes.h"
22 /** Generation of the identity matrix for a given dimension **/
24 Matrix
*matrixIdentity(int dimension
){
25 Matrix
*result
=Matrix_Alloc(dimension
,dimension
);
28 for(i
=0;i
<dimension
;i
++)
29 for(j
=0;j
<dimension
;j
++)
30 value_assign(result
->p
[i
][j
],int_to_value((i
==j
)?1:0));
34 /** Matrices addition (with coefficients associated to each matrix) **/
36 Matrix
*matricesAddition(Matrix
*m1
,Matrix
*m2
,int coeff1
,int coeff2
)
38 int NbRows1
,NbColumns1
;
39 int NbRows2
,NbColumns2
;
40 Value val1
=int_to_value(coeff1
);
41 Value val2
=int_to_value(coeff2
);
45 /* Handle NULL matrices */
46 if(!m1
||!(m1
->p
)||!m2
||!(m2
->p
)) return NULL
;
48 /* Matrices sizes reading */
50 NbColumns1
= m1
->NbColumns
;
52 NbColumns2
= m2
->NbColumns
;
54 /* Verify validity of the composition */
55 if(NbRows1
!=NbRows2
||NbColumns1
!=NbColumns2
){
56 errModuleSet("COMMON");
57 errError(INC_DIMENSIONS
,TERROR
,"matricesAddition",NULL
);
60 /* Creation of a new matrix */
61 result
=Matrix_Alloc(NbRows1
,NbColumns1
);
64 for(i
=0;i
<NbRows1
;i
++)
65 for(j
=0;j
<NbColumns1
;j
++)
68 value_plus(value_mult(val1
,m1
->p
[i
][j
]),
69 value_mult(val2
,m2
->p
[i
][j
])));
74 /** Matrices multiplication (in fact affine functions composition) **/
76 Matrix
*matricesMultiplication(Matrix
*m1
,Matrix
*m2
)
78 int NbRows1
,NbColumns1
;
79 int NbRows2
,NbColumns2
;
83 /* Handle NULL matrices */
84 if(!m1
||!(m1
->p
)||!m2
||!(m2
->p
)) return NULL
;
86 /* Matrices sizes reading */
88 NbColumns1
= m1
->NbColumns
;
90 NbColumns2
= m2
->NbColumns
;
92 /* Verify validity of the composition */
93 if(NbColumns1
!=NbRows2
){
94 errModuleSet("COMMON");
95 errError(INC_DIMENSIONS
,TERROR
,"matricesMultiplication",NULL
);
98 /* Creation of a new matrix */
99 result
=Matrix_Alloc(NbRows1
,NbColumns2
);
101 /* The multiplication */
102 for(i
=0;i
<NbRows1
;i
++)
103 for(j
=0;j
<NbColumns2
;j
++) {
104 value_assign(result
->p
[i
][j
],int_to_value(0));
105 for(k
=0;k
<NbColumns1
;k
++)
106 value_addto(result
->p
[i
][j
], result
->p
[i
][j
],
107 value_mult(m1
->p
[i
][k
],m2
->p
[k
][j
]));
114 /** Concatenate two matrices (one below the other) **/
115 /** The matrices must have the same number of cols **/
117 Matrix
*matricesConcatenate(Matrix
*m1
,Matrix
*m2
)
119 int rows1
=m1
->NbRows
,rows2
=m2
->NbRows
;
120 int cols1
=m1
->NbColumns
,cols2
=m2
->NbColumns
;
124 if(cols1
!=cols2
) return NULL
;
125 result
=Matrix_Alloc(rows1
+rows2
,cols1
);
128 value_assign(result
->p
[i
][j
],m1
->p
[i
][j
]);
131 value_assign(result
->p
[rows1
+i
][j
],m2
->p
[i
][j
]);