ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1// Copyright 2018-2023 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef _dspm_mat_h_
16#define _dspm_mat_h_
17#include <iostream>
18
24namespace dspm {
30class Mat {
31public:
32
33 int rows;
34 int cols;
35 int stride;
36 int padding;
37 float *data;
38 int length;
39 static float abs_tol;
40 bool ext_buff;
42
48 struct Rect {
49 int x;
50 int y;
51 int width;
52 int height;
53
62 Rect(int x = 0, int y = 0, int width = 0, int height = 0);
63
72 void resizeRect(int x, int y, int width, int height);
73
77 int areaRect(void);
78 };
79
85 Mat(int rows, int cols);
92 Mat(float *data, int rows, int cols);
93
101 Mat(float *data, int rows, int cols, int stride);
102
106 Mat();
107 virtual ~Mat();
108
117 Mat(const Mat &src);
118
130 Mat getROI(int startRow, int startCol, int roiRows, int roiCols);
131
144 Mat getROI(int startRow, int startCol, int roiRows, int roiCols, int stride);
145
154 Mat getROI(const Mat::Rect &rect);
155
162 void Copy(const Mat &src, int row_pos, int col_pos);
163
170 void CopyHead(const Mat &src);
171
178 void PrintHead(void);
179
190 Mat Get(int row_start, int row_size, int col_start, int col_size);
191
198 Mat Get(const Mat::Rect &rect);
199
208 Mat &operator=(const Mat &src);
209
218 inline float &operator()(int row, int col)
219 {
220 return data[row * this->stride + col];
221 }
222
230 inline const float &operator()(int row, int col) const
231 {
232 return data[row * this->stride + col];
233 }
234
244 Mat &operator+=(const Mat &A);
245
255 Mat &operator+=(float C);
265 Mat &operator-=(const Mat &A);
266
276 Mat &operator-=(float C);
277
287 Mat &operator*=(const Mat &A);
297 Mat &operator*=(float C);
307 Mat &operator/=(float C);
316 Mat &operator/=(const Mat &B);
325 Mat operator^(int C);
326
332 void swapRows(int row1, int row2);
340 Mat t();
341
351 static Mat eye(int size);
352
362 static Mat ones(int size);
363
374 static Mat ones(int rows, int cols);
375
387 Mat block(int startRow, int startCol, int blockRows, int blockCols);
388
394 void normalize(void);
395
403 float norm(void);
404
409 void clear(void);
410
422 static Mat solve(Mat A, Mat b);
435 static Mat bandSolve(Mat A, Mat b, int k);
447 static Mat roots(Mat A, Mat y);
448
460 static float dotProduct(Mat A, Mat B);
461
473 static Mat augment(Mat A, Mat B);
483
491
498 Mat inverse();
499
506 Mat pinv();
507
515 float det(int n);
516private:
517 Mat cofactor(int row, int col, int n);
518 Mat adjoint();
519
520 void allocate(); // Allocate buffer
521 Mat expHelper(const Mat &m, int num);
522};
523
531std::ostream &operator<<(std::ostream &os, const Mat &m);
532
541std::ostream &operator<<(std::ostream &os, const Mat::Rect &rect);
542
551std::istream &operator>>(std::istream &is, Mat &m);
552
563Mat operator+(const Mat &A, const Mat &B);
574Mat operator+(const Mat &A, float C);
575
586Mat operator-(const Mat &A, const Mat &B);
597Mat operator-(const Mat &A, float C);
598
609Mat operator*(const Mat &A, const Mat &B);
610
621Mat operator*(const Mat &A, float C);
622
633Mat operator*(float C, const Mat &A);
634
645Mat operator/(const Mat &A, float C);
646
656Mat operator/(const Mat &A, const Mat &B);
657
668bool operator==(const Mat &A, const Mat &B);
669
670}
671#endif //_dspm_mat_h_
Matrix.
Definition mat.h:30
Mat Get(int row_start, int row_size, int col_start, int col_size)
Definition mat.cpp:209
Mat & operator+=(const Mat &A)
Definition mat.cpp:262
float det(int n)
Definition mat.cpp:746
static float abs_tol
Definition mat.h:39
Mat operator^(int C)
Definition mat.cpp:364
Mat rowReduceFromGaussian()
Definition mat.cpp:656
static Mat ones(int size)
Definition mat.cpp:409
void PrintHead(void)
print matrix header
Definition mat.cpp:197
int stride
Definition mat.h:35
Mat adjoint()
Definition mat.cpp:783
float * data
Definition mat.h:37
void normalize(void)
Definition mat.cpp:444
float norm(void)
Definition mat.cpp:456
Mat pinv()
Definition mat.cpp:707
Mat()
Definition mat.cpp:98
static float dotProduct(Mat A, Mat B)
Dotproduct of two vectors.
Definition mat.cpp:576
int cols
Definition mat.h:34
Mat(int rows, int cols)
Definition mat.cpp:69
bool ext_buff
Definition mat.h:40
void clear(void)
Definition mat.cpp:425
Mat & operator=(const Mat &src)
Definition mat.cpp:231
Mat & operator*=(const Mat &A)
Definition mat.cpp:312
int length
Definition mat.h:38
static Mat eye(int size)
Definition mat.cpp:394
Mat expHelper(const Mat &m, int num)
Definition mat.cpp:842
Mat block(int startRow, int startCol, int blockRows, int blockCols)
Definition mat.cpp:433
virtual ~Mat()
Definition mat.cpp:111
static Mat bandSolve(Mat A, Mat b, int k)
Band solve the matrix.
Definition mat.cpp:514
bool sub_matrix
Definition mat.h:41
int padding
Definition mat.h:36
void Copy(const Mat &src, int row_pos, int col_pos)
Definition mat.cpp:168
void swapRows(int row1, int row2)
Definition mat.cpp:370
void CopyHead(const Mat &src)
copy header of matrix
Definition mat.cpp:182
Mat getROI(int startRow, int startCol, int roiRows, int roiCols)
Create a subset of matrix as ROI (Region of Interest).
Definition mat.cpp:163
void allocate()
Definition mat.cpp:834
int rows
Definition mat.h:33
Mat & operator/=(float C)
Definition mat.cpp:354
float & operator()(int row, int col)
Definition mat.h:218
Mat gaussianEliminate()
Gaussian Elimination.
Definition mat.cpp:600
static Mat augment(Mat A, Mat B)
Augmented matrices.
Definition mat.cpp:585
static Mat solve(Mat A, Mat b)
Solve the matrix.
Definition mat.cpp:468
const float & operator()(int row, int col) const
Definition mat.h:230
Mat cofactor(int row, int col, int n)
Definition mat.cpp:722
Mat t()
Definition mat.cpp:383
static Mat roots(Mat A, Mat y)
Solve the matrix.
Definition mat.cpp:552
Mat inverse()
Definition mat.cpp:812
Mat & operator-=(const Mat &A)
Definition mat.cpp:287
DSP matrix namespace.
Definition mat.h:24
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)
Definition mat.cpp:903
Mat operator+(const Mat &A, const Mat &B)
Definition mat.cpp:855
bool operator==(const Mat &A, const Mat &B)
Definition mat.cpp:885
Mat operator/(const Mat &A, float C)
Definition mat.cpp:969
Mat operator*(const Mat &A, const Mat &B)
Definition mat.cpp:933
Rectangular area.
Definition mat.h:48
int width
Definition mat.h:51
int height
Definition mat.h:52
void resizeRect(int x, int y, int width, int height)
Resize rect area.
Definition mat.cpp:44
Rect(int x=0, int y=0, int width=0, int height=0)
Constructor with initialization to 0.
Definition mat.cpp:36
int areaRect(void)
Get amount of elements in the rect area.
Definition mat.cpp:52
float y[1024]
Definition test_fir.c:11
float C[4][16]
Definition test_mmult.c:22
const int m
Definition test_mmult.c:16
float B[8][16]
Definition test_mmult.c:21
float A[4][8]
Definition test_mmult.c:20
const int n
Definition test_mmult.c:17
const int k
Definition test_mmult.c:18