combine .cvsignore files into .gitignore file
[sppoc.git] / CipolCore / Common / matrices.c
blob7d44fd39e8693be075c9790e6193dd32061bca9c
1 /**************************************************/
2 /* Some common operations on matrices. */
3 /**************************************************/
5 /*** Include files ***/
7 #include "stdio.h"
8 #include "stdlib.h"
9 #include "stddef.h"
11 #include "comtypes.h"
12 #include "../cipol.h"
13 #include "../erroridt.h"
14 #include "../Polylib/polytypes.h"
16 #ifdef DBMALLOC
17 # include "malloc.h"
18 #endif
20 /*** Functions ***/
22 /** Generation of the identity matrix for a given dimension **/
24 Matrix *matrixIdentity(int dimension){
25 Matrix *result=Matrix_Alloc(dimension,dimension);
26 int i,j;
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));
31 return result;
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);
42 Matrix *result;
43 int i,j;
45 /* Handle NULL matrices */
46 if(!m1||!(m1 ->p)||!m2||!(m2->p)) return NULL;
48 /* Matrices sizes reading */
49 NbRows1 = m1->NbRows;
50 NbColumns1 = m1->NbColumns;
51 NbRows2 = m2->NbRows;
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);
63 /* Do the addition */
64 for(i=0;i<NbRows1;i++)
65 for(j=0;j<NbColumns1;j++)
66 value_assign(
67 result->p[i][j],
68 value_plus(value_mult(val1,m1->p[i][j]),
69 value_mult(val2,m2->p[i][j])));
71 return result;
74 /** Matrices multiplication (in fact affine functions composition) **/
76 Matrix *matricesMultiplication(Matrix *m1,Matrix *m2)
78 int NbRows1,NbColumns1;
79 int NbRows2,NbColumns2;
80 Matrix *result;
81 int i,j,k;
83 /* Handle NULL matrices */
84 if(!m1||!(m1 ->p)||!m2||!(m2->p)) return NULL;
86 /* Matrices sizes reading */
87 NbRows1 = m1->NbRows;
88 NbColumns1 = m1->NbColumns;
89 NbRows2 = m2->NbRows;
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]));
110 /* Return result */
111 return(result);
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;
121 Matrix *result;
122 int i,j;
124 if(cols1!=cols2) return NULL;
125 result=Matrix_Alloc(rows1+rows2,cols1);
126 for(i=0;i<rows1;i++)
127 for(j=0;j<cols1;j++)
128 value_assign(result->p[i][j],m1->p[i][j]);
129 for(i=0;i<rows2;i++)
130 for(j=0;j<cols2;j++)
131 value_assign(result->p[rows1+i][j],m2->p[i][j]);
132 return result;