Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Eigen
matrix.cpp
00001 #include "matrix.h" 00002 using namespace std; 00003 00004 matrix::matrix(uint8_t I,uint8_t J,float e) 00005 { 00006 this->I = I; 00007 this->J = J; 00008 a = (float **)malloc(I * sizeof(float *)); 00009 for (uint8_t i=0; i<I; i++) 00010 a[i] = (float *)malloc(J * sizeof(float)); 00011 for(uint8_t i=0;i<I;i++) 00012 for(uint8_t j=0;j<J;j++) 00013 a[i][j]=e;//;*/ 00014 } 00015 // create Unity matrix 00016 matrix::matrix(uint8_t IJ) 00017 { 00018 this->I = IJ; 00019 this->J = IJ; 00020 a = (float **)malloc(IJ * sizeof(float *)); 00021 for (uint8_t i=0; i<IJ; i++) 00022 a[i] = (float *)malloc(IJ * sizeof(float)); 00023 for(uint8_t i=0;i<IJ;i++) 00024 for(uint8_t j=0;j<IJ;j++) 00025 a[i][j]=0.0;//;*/ 00026 for(uint8_t i=0;i<IJ;i++) 00027 a[i][i]=1.0;//;*/ 00028 } 00029 // create diagonal matrix with Entry e 00030 matrix::matrix(uint8_t IJ,float e) 00031 { 00032 this->I = IJ; 00033 this->J = IJ; 00034 a = (float **)malloc(IJ * sizeof(float *)); 00035 for (uint8_t i=0; i<IJ; i++) 00036 a[i] = (float *)malloc(IJ * sizeof(float)); 00037 for(uint8_t i=0;i<IJ;i++) 00038 for(uint8_t j=0;j<IJ;j++) 00039 a[i][j]=0.0;//;*/ 00040 for(uint8_t i=0;i<IJ;i++) 00041 a[i][i]=e;//;*/ 00042 } 00043 00044 // display matrix on screen 00045 void matrix::printout(void) 00046 { 00047 for(uint8_t i=0;i<I;i++) 00048 { 00049 for(uint8_t j=0;j<J;j++) 00050 printf("%.8e ",a[i][j]); 00051 printf("..\r\n"); 00052 } 00053 } 00054 00055 matrix::~matrix(void) { 00056 for (uint8_t i=0; i<I; i++) 00057 delete a[i]; 00058 delete [] a; 00059 // printf("Matrix is being deleted\r\n"); 00060 } 00061 uint8_t matrix::getI(void) 00062 {return I;} 00063 uint8_t matrix::getJ(void) 00064 {return J;} 00065 00066 00067 matrix matrix::operator*(const matrix& B) { 00068 if (J != B.I) { 00069 printf("Matrices shapes mismatch"); 00070 } 00071 matrix C(I, B.J,0.0); 00072 for(uint8_t i=0;i<I;i++) 00073 for(uint8_t j=0;j<B.J;j++) 00074 for(uint8_t k=0;k<J;k++) 00075 C.a[i][j]+=(a[i][k]*B.a[k][j]); 00076 return C; 00077 } 00078 // calculate A'*B 00079 matrix matrix::operator *=(const matrix& B) { 00080 if (I != B.I) { 00081 printf("Matrices shapes mismatch"); 00082 } 00083 matrix C(J, B.J,0.0); 00084 for(uint8_t j1=0;j1<J;j1++) 00085 for(uint8_t j=0;j<B.J;j++) 00086 for(uint8_t k=0;k<I;k++) 00087 C.a[j1][j]+=(a[k][j1]*B.a[k][j]); 00088 return C; 00089 } 00090 // calculate A*B' 00091 matrix matrix::operator /=(const matrix& B) { 00092 if (J != B.J) { 00093 printf("Matrices shapes mismatch"); 00094 } 00095 matrix C(I, B.I,0.0); 00096 for(uint8_t i=0;i<I;i++) 00097 for(uint8_t i1=0;i1<B.I;i1++) 00098 for(uint8_t k=0;k<J;k++) 00099 C.a[i][i1]+=(a[i][k]*B.a[i1][k]); 00100 return C; 00101 } 00102 // calc Matrix C=A+B 00103 matrix matrix::operator+(const matrix& B) { 00104 matrix C(I, J,0.0); 00105 for(uint8_t i=0;i<I;i++) 00106 for(uint8_t j=0;j<B.J;j++) 00107 C.a[i][j]=(a[i][j]+B.a[i][j]); 00108 return C; 00109 } 00110 // calc Matrix A=A+B 00111 void matrix::operator+=(const matrix& B) { 00112 for(uint8_t i=0;i<I;i++) 00113 for(uint8_t j=0;j<B.J;j++) 00114 a[i][j]+=B.a[i][j]; 00115 } 00116 // calc Matrix C=A-B 00117 matrix matrix::operator-(const matrix& B) { 00118 matrix C(I, J,0.0); 00119 for(uint8_t i=0;i<I;i++) 00120 for(uint8_t j=0;j<B.J;j++) 00121 C.a[i][j]=(a[i][j]-B.a[i][j]); 00122 return C; 00123 } 00124 00125 // calc Matrix inv(A), A must be 2x2 00126 matrix matrix::inv_2x2(void) { 00127 matrix C(2, 2,0.0); 00128 float idet=1/(a[0][0]*a[1][1]-a[0][1]*a[1][0]); 00129 C.a[0][0] = idet * a[1][1]; 00130 C.a[0][1] = -idet * a[0][1]; 00131 C.a[1][0] = -idet * a[1][0]; 00132 C.a[1][1] = idet * a[0][0]; 00133 return C; 00134 } 00135 void matrix::scale(float sc) 00136 { 00137 for(uint8_t i=0;i<I;i++) 00138 for(uint8_t j=0;j<J;j++) 00139 a[i][j]*=sc; 00140 } 00141 void matrix::put_entry(uint8_t i,uint8_t j,float e) 00142 { 00143 a[i][j]=e; 00144 } 00145 // Fill Column 00146 void matrix::fill_row(uint8_t i,float *e) 00147 { 00148 for(uint8_t j=0;j<J;j++) 00149 a[i][j]=e[j]; 00150 } 00151 00152 // Fill Row 00153 void matrix::fill_col(uint8_t j,float *e) 00154 { 00155 for(uint8_t i=0;i<I;i++) 00156 a[i][j]=e[i]; 00157 } 00158 // Copy matrix 00159 void matrix::mcopy(matrix *B) 00160 { 00161 for(uint8_t i=0;i<I;i++) 00162 for(uint8_t j=0;j<J;j++) 00163 a[i][j]=B->a[i][j]; 00164 00165 }
Generated on Thu Jul 14 2022 22:08:52 by
1.7.2