59 this->
rows = roi_rows;
60 this->
cols = roi_cols;
78 memset(this->
data, 0, this->
length *
sizeof(
float));
89 this->
length = this->rows * this->cols;
92 for (
size_t i = 0; i < this->
length; i++) {
93 this->data[i] =
data[i];
134 memcpy(this->
data,
m.data, this->length *
sizeof(
float));
140 Mat result(this->
data, roiRows, roiCols, 0);
142 if ((startRow + roiRows) > this->
rows) {
145 if ((startCol + roiCols) > this->
cols) {
149 const int ptr_move = startRow * this->
cols + startCol;
150 float *new_data_ptr = this->
data + ptr_move;
152 result.
data = new_data_ptr;
165 return (
getROI(startRow, startCol, roiRows, roiCols, this->
cols));
170 if ((row_pos + src.
rows) > this->rows) {
173 if ((col_pos + src.
cols) > this->cols) {
177 for (
size_t r = 0; r < src.
rows; r++) {
178 memcpy(&this->
data[(r + row_pos) * this->
stride + col_pos], &src.
data[r * src.
cols], src.
cols *
sizeof(
float));
199 std::cout <<
"rows " << this->
rows << std::endl;
200 std::cout <<
"cols " << this->
cols << std::endl;
201 std::cout <<
"lenght " << this->
length << std::endl;
202 std::cout <<
"data " << this->
data << std::endl;
203 std::cout <<
"ext_buff " << this->
ext_buff << std::endl;
204 std::cout <<
"sub_mat " << this->
sub_matrix << std::endl;
205 std::cout <<
"stride " << this->
stride << std::endl;
206 std::cout <<
"padding " << this->
padding << std::endl << std::endl;
209Mat Mat::Get(
int row_start,
int row_size,
int col_start,
int col_size)
211 Mat result(row_size, col_size);
213 if ((row_start + row_size) > this->
rows) {
216 if ((col_start + col_size) > this->
cols) {
220 for (
size_t r = 0; r < result.
rows; r++) {
221 memcpy(&result.
data[r * result.
cols], &this->data[(r + row_start) * this->stride + col_start], result.
cols *
sizeof(
float));
238 if (this->
rows !=
m.rows || this->cols !=
m.cols) {
241 ESP_LOGE(
"Mat",
"operator = Error for sub-matrices: operands matrices dimensions %dx%d and %dx%d do not match", this->
rows, this->
cols,
m.rows,
m.cols);
256 for (
int row = 0; row < this->
rows; row++) {
257 memcpy(this->
data + (row * this->
stride),
m.data + (row *
m.stride), this->cols *
sizeof(
float));
264 if ((this->
rows !=
m.rows) || (this->cols !=
m.cols)) {
265 ESP_LOGW(
"Mat",
"operator += Error: matrices do not have equal dimensions");
270 dspm_add_f32(this->
data,
m.data, this->data, this->rows, this->cols, this->padding,
m.padding, this->padding, 1, 1, 1);
289 if ((this->
rows !=
m.rows) || (this->cols !=
m.cols)) {
290 ESP_LOGW(
"Mat",
"operator -= Error: matrices do not have equal dimensions");
295 dspm_sub_f32(this->
data,
m.data, this->data, this->rows, this->cols, this->padding,
m.padding, this->padding, 1, 1, 1);
314 if (this->
cols !=
m.rows) {
315 ESP_LOGW(
"Mat",
"operator *= Error: matrices do not have equal dimensions");
341 if ((this->
rows !=
B.rows) || (this->cols !=
B.cols)) {
342 ESP_LOGW(
"Mat",
"operator /= Error: matrices do not have equal dimensions");
346 for (
int row = 0; row < this->
rows; row++) {
347 for (
int col = 0; col < this->
cols; col++) {
348 (*this)(row, col) = (*
this)(row, col) /
B(row, col);
372 if ((this->
rows <= r1) || (this->
rows <= r2)) {
373 ESP_LOGW(
"Mat",
"swapRows Error: row %d or %d out of matrix row %d range", r1, r2, this->
rows);
375 for (
int i = 0; i < this->
cols; i++) {
376 float temp = this->
data[r1 * this->
stride + i];
386 for (
int i = 0; i < this->
rows; ++i) {
387 for (
int j = 0; j < this->
cols; ++j) {
396 Mat temp(size, size);
397 for (
int i = 0; i < temp.
rows; ++i) {
398 for (
int j = 0; j < temp.
cols; ++j) {
411 return (
ones(size, size));
417 for (
int row = 0; row < temp.
rows; ++row) {
418 for (
int col = 0; col < temp.
cols; ++col) {
427 for (
int row = 0; row < this->
rows; row++) {
428 memset(this->
data + (row * this->
stride), 0, this->
cols *
sizeof(
float));
435 Mat result(blockRows, blockCols);
436 for (
int i = 0; i < blockRows; ++i) {
437 for (
int j = 0; j < blockCols; ++j) {
438 result(i, j) = (*this)(startRow + i, startCol + j);
447 for (
int i = 0; i < this->
rows; ++i) {
448 for (
int j = 0; j < this->
cols; ++j) {
449 sqr_norm += (*this)(i, j) * (*
this)(i, j);
452 sqr_norm = 1 / sqrtf(sqr_norm);
459 for (
int i = 0; i < this->
rows; ++i) {
460 for (
int j = 0; j < this->
cols; ++j) {
461 sqr_norm += (*this)(i, j) * (*
this)(i, j);
464 sqr_norm = sqrtf(sqr_norm);
471 for (
int i = 0; i <
A.rows; ++i) {
474 ESP_LOGW(
"Mat",
"Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
475 Mat err_result(0, 0);
478 float a_ii = 1 /
A(i, i);
479 for (
int j = i + 1; j <
A.rows; ++j) {
480 float a_ji =
A(j, i) * a_ii;
481 for (
int k = i + 1;
k <
A.cols; ++
k) {
482 A(j,
k) -=
A(i,
k) * a_ji;
487 b(j, 0) -= b(i, 0) * a_ji;
497 x((
x.rows - 1), 0) = b((
x.rows - 1), 0) /
A((
x.rows - 1), (
x.rows - 1));
499 x((
x.rows - 1), 0) = 0;
501 for (
int i =
x.rows - 2; i >= 0; --i) {
503 for (
int j = i + 1; j <
x.rows; ++j) {
504 sum +=
A(i, j) *
x(j, 0);
506 x(i, 0) = (b(i, 0) - sum) /
A(i, i);
517 int bandsBelow = (
k - 1) / 2;
518 for (
int i = 0; i <
A.rows; ++i) {
521 ESP_LOGW(
"Mat",
"Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
523 memset(err_result.
data, 0, b.
rows *
sizeof(
float));
526 float a_ii = 1 /
A(i, i);
527 for (
int j = i + 1; j <
A.rows && j <= i + bandsBelow; ++j) {
529 while ((
k <
A.cols) && (fabs(
A(j,
k)) >
abs_tol)) {
530 A(j,
k) -=
A(i,
k) * (
A(j, i) * a_ii);
533 b(j, 0) -= b(i, 0) * (
A(j, i) * a_ii);
540 x((
x.rows - 1), 0) = b((
x.rows - 1), 0) /
A((
x.rows - 1), (
x.rows - 1));
541 for (
int i =
x.rows - 2; i >= 0; --i) {
543 for (
int j = i + 1; j <
x.rows; ++j) {
544 sum +=
A(i, j) *
x(j, 0);
546 x(i, 0) = (b(i, 0) - sum) /
A(i, i);
556 Mat result(
y.rows, 1);
559 for (
int j = 0; j <
A.cols; j++) {
560 float g_jj = 1 / g_m(j, j);
561 for (
int i = 0; i <
A.cols; i++) {
563 float c = g_m(i, j) * g_jj;
564 for (
int k = 0;
k <
n;
k++) {
565 g_m(i,
k) = g_m(i,
k) - c * g_m(j,
k);
570 for (
int i = 0; i <
A.rows; i++) {
571 result(i, 0) = g_m(i,
A.cols) / g_m(i, i);
579 for (
int i = 0; i < a.
rows; ++i) {
580 sum += (a(i, 0) * b(i, 0));
587 Mat AB(
A.rows,
A.cols +
B.cols);
588 for (
int i = 0; i < AB.
rows; ++i) {
589 for (
int j = 0; j < AB.
cols; ++j) {
593 AB(i, j) =
B(i, j -
A.cols);
605 int Acols =
cols - 1;
613 bool pivot_found =
false;
614 while (j < Acols && !pivot_found) {
620 for (
int k = i + 1;
k <
rows; ++
k) {
621 float cur_abs = Ab(
k, j) >= 0 ? Ab(
k, j) : -1 * Ab(
k, j);
622 if (cur_abs > max_val) {
638 for (
int t = i + 1;
t <
rows; ++
t) {
639 for (
int s = j + 1; s <
cols; ++s) {
640 Ab(
t, s) = Ab(
t, s) - Ab(i, s) * (Ab(
t, j) / Ab(i, j));
678 for (
int t = i - 1;
t >= 0; --
t) {
679 for (
int s = 0; s <
cols; ++s) {
681 R(
t, s) = R(
t, s) - R(i, s) * (R(
t, j) / R(i, j));
691 for (
int k = j + 1;
k <
cols; ++
k) {
692 R(i,
k) = R(i,
k) / R(i, j);
714 for (
int i = 0; i < this->
rows; ++i) {
715 for (
int j = 0; j < this->
cols; ++j) {
716 AInverse(i, j) = IAInverse(i, j + this->cols);
727 for (
int r = 0; r <
n; r++) {
728 for (
int c = 0; c <
n; c++) {
731 if (r != row && c != col) {
732 result(i, j++) = (*this)(r, c);
736 if (j == this->
rows - 1) {
750 return (*
this)(0, 0);
757 for (
int i = 0; i <
n; i++) {
759 for (
int j = i + 1; j <
n; j++) {
760 if (std::abs((*temp)(j, i)) > std::abs((*temp)(pivot, i))) {
768 if ((*temp)(i, i) == 0) {
771 det *= (*temp)(i, i);
772 for (
int j = i + 1; j <
n; j++) {
773 float factor = (*temp)(j, i) / (*temp)(i, i);
774 for (
int k = i + 1;
k <
n;
k++) {
775 (*temp)(j,
k) -= factor * (*temp)(i,
k);
786 if (this->
rows == 1) {
795 for (
int i = 0; i < this->
rows; i++) {
796 for (
int j = 0; j < this->
cols; j++) {
798 temp = this->
cofactor( i, j, this->rows);
802 sign = ((i + j) % 2 == 0) ? 1 : -1;
806 adj(j, i) = (sign) * (temp.
det(this->rows - 1));
826 for (
int i = 0; i < this->
rows; i++)
827 for (
int j = 0; j < this->
cols; j++) {
828 result(i, j) = adj(i, j) / float(
det);
846 }
else if (num == 1) {
848 }
else if (num % 2 == 0) {
858 ESP_LOGW(
"Mat",
"operator + Error: matrices do not have equal dimensions");
865 dspm_add_f32(m1.
data, m2.
data, temp.
data, m1.
rows, m1.
cols, m1.
padding, m2.
padding, temp.
padding, 1, 1, 1);
876 Mat temp(
m.rows,
m.cols);
891 for (
int row = 0; row < m1.
rows; row++) {
892 for (
int col = 0; col < m1.
cols; col++) {
893 if (m1(row, col) != m2(row, col)) {
894 ESP_LOGW(
"Mat",
"operator == Error: %i %i, m1.data=%f, m2.data=%f \n", row, col, m1(row, col), m2(row, col));
906 ESP_LOGW(
"Mat",
"operator - Error: matrices do not have equal dimensions");
913 dspm_sub_f32(m1.
data, m2.
data, temp.
data, m1.
rows, m1.
cols, m1.
padding, m2.
padding, temp.
padding, 1, 1, 1);
924 Mat temp(
m.rows,
m.cols);
936 ESP_LOGW(
"Mat",
"operator * Error: matrices do not have correct dimensions");
955 Mat temp(
m.rows,
m.cols);
960 return (temp *= num);
972 Mat temp(
m.rows,
m.cols);
977 return (temp /= num);
983 if ((
A.rows !=
B.rows) || (
A.cols !=
B.cols)) {
984 ESP_LOGW(
"Mat",
"Operator + Error: matrices do not have equal dimensions");
989 Mat temp(
A.rows,
A.cols);
990 for (
int row = 0; row <
A.rows; row++) {
991 for (
int col = 0; col <
A.cols; col++) {
992 temp(row, col) =
A(row, col) /
B(row, col);
1000 for (
int i = 0; i <
m.rows; ++i) {
1002 for (
int j = 1; j <
m.cols; ++j) {
1003 os <<
" " <<
m(i, j);
1012 os <<
"row start " << rect.
y << endl;
1013 os <<
"col start " << rect.
x << endl;
1014 os <<
"row count " << rect.
height << endl;
1015 os <<
"col count " << rect.
width << endl;
1022 for (
int i = 0; i <
m.rows; ++i) {
1023 for (
int j = 0; j <
m.cols; ++j) {
Mat Get(int row_start, int row_size, int col_start, int col_size)
Mat & operator+=(const Mat &A)
Mat rowReduceFromGaussian()
static Mat ones(int size)
void PrintHead(void)
print matrix header
static float dotProduct(Mat A, Mat B)
Dotproduct of two vectors.
Mat & operator=(const Mat &src)
Mat & operator*=(const Mat &A)
Mat expHelper(const Mat &m, int num)
Mat block(int startRow, int startCol, int blockRows, int blockCols)
static Mat bandSolve(Mat A, Mat b, int k)
Band solve the matrix.
void Copy(const Mat &src, int row_pos, int col_pos)
void swapRows(int row1, int row2)
void CopyHead(const Mat &src)
copy header of matrix
Mat getROI(int startRow, int startCol, int roiRows, int roiCols)
Create a subset of matrix as ROI (Region of Interest).
Mat & operator/=(float C)
Mat gaussianEliminate()
Gaussian Elimination.
static Mat augment(Mat A, Mat B)
Augmented matrices.
static Mat solve(Mat A, Mat b)
Solve the matrix.
Mat cofactor(int row, int col, int n)
static Mat roots(Mat A, Mat y)
Solve the matrix.
Mat & operator-=(const Mat &A)
esp_err_t dsps_addc_f32_ansi(const float *input, float *output, int len, float C, int step_in, int step_out)
add constant
esp_err_t dsps_mulc_f32_ansi(const float *input, float *output, int len, float C, int step_in, int step_out)
multiply constant
std::istream & operator>>(std::istream &is, Mat &m)
std::ostream & operator<<(std::ostream &os, const Mat &m)
Mat operator-(const Mat &A, const Mat &B)
Mat operator+(const Mat &A, const Mat &B)
bool operator==(const Mat &A, const Mat &B)
Mat operator/(const Mat &A, float C)
Mat operator*(const Mat &A, const Mat &B)
void resizeRect(int x, int y, int width, int height)
Resize rect area.
Rect(int x=0, int y=0, int width=0, int height=0)
Constructor with initialization to 0.
int areaRect(void)
Get amount of elements in the rect area.