![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo program for Array class and Matrix class.
Dependencies: Array_Matrix mbed
main.cpp@3:2649800966bb, 2019-11-19 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Nov 19 05:45:33 2019 +0000
- Revision:
- 3:2649800966bb
- Parent:
- 1:3df59f65bdcc
- Child:
- 4:c6d4d3e9ddd7
4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:90a30b7b0f2f | 1 | //------------------------------------------------------------ |
MikamiUitOpen | 0:90a30b7b0f2f | 2 | // Demo program for Array class and Matrix class |
MikamiUitOpen | 0:90a30b7b0f2f | 3 | // |
MikamiUitOpen | 3:2649800966bb | 4 | // 2019/11/19, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:90a30b7b0f2f | 5 | //------------------------------------------------------------ |
MikamiUitOpen | 0:90a30b7b0f2f | 6 | |
MikamiUitOpen | 0:90a30b7b0f2f | 7 | #define DEBUG_ARRAY_CHECK // Range check of index available |
MikamiUitOpen | 0:90a30b7b0f2f | 8 | |
MikamiUitOpen | 0:90a30b7b0f2f | 9 | #include "Matrix.hpp" |
MikamiUitOpen | 0:90a30b7b0f2f | 10 | #include <string> |
MikamiUitOpen | 0:90a30b7b0f2f | 11 | |
MikamiUitOpen | 0:90a30b7b0f2f | 12 | using namespace Mikami; |
MikamiUitOpen | 0:90a30b7b0f2f | 13 | void PrintMatrix(const char str[], const Matrix<int> &x); |
MikamiUitOpen | 0:90a30b7b0f2f | 14 | |
MikamiUitOpen | 0:90a30b7b0f2f | 15 | void Square(Array<float> & x); |
MikamiUitOpen | 0:90a30b7b0f2f | 16 | void Sqrt(float x[], int N); |
MikamiUitOpen | 0:90a30b7b0f2f | 17 | |
MikamiUitOpen | 0:90a30b7b0f2f | 18 | int main() |
MikamiUitOpen | 0:90a30b7b0f2f | 19 | { |
MikamiUitOpen | 0:90a30b7b0f2f | 20 | // Test of Array class |
MikamiUitOpen | 0:90a30b7b0f2f | 21 | Array<int> sn(10); |
MikamiUitOpen | 0:90a30b7b0f2f | 22 | sn[9] = 0; |
MikamiUitOpen | 3:2649800966bb | 23 | // sn[10] = 0; // Out of range error |
MikamiUitOpen | 0:90a30b7b0f2f | 24 | |
MikamiUitOpen | 3:2649800966bb | 25 | // Array<uint8_t> a1(1000000); // Size error (Nucleo-F446RE) |
MikamiUitOpen | 0:90a30b7b0f2f | 26 | Array<uint8_t> a1(10000); |
MikamiUitOpen | 3:2649800966bb | 27 | printf("\r\nMemory allocation: OK\r\n"); |
MikamiUitOpen | 0:90a30b7b0f2f | 28 | |
MikamiUitOpen | 0:90a30b7b0f2f | 29 | const int N1 = 10; |
MikamiUitOpen | 0:90a30b7b0f2f | 30 | Array<float> x1(N1), x2, x3; |
MikamiUitOpen | 0:90a30b7b0f2f | 31 | for (int n=0; n<N1; n++) x1[n] = n; |
MikamiUitOpen | 0:90a30b7b0f2f | 32 | x3 = x2 = x1; |
MikamiUitOpen | 0:90a30b7b0f2f | 33 | |
MikamiUitOpen | 0:90a30b7b0f2f | 34 | Square(x2); |
MikamiUitOpen | 0:90a30b7b0f2f | 35 | Sqrt(x3, N1); |
MikamiUitOpen | 0:90a30b7b0f2f | 36 | for (int n=0; n<N1; n++) |
MikamiUitOpen | 0:90a30b7b0f2f | 37 | printf("%8.4f, %8.4f, %8.4f\r\n", x1[n], x2[n], x3[n]); |
MikamiUitOpen | 0:90a30b7b0f2f | 38 | printf("\r\n"); |
MikamiUitOpen | 0:90a30b7b0f2f | 39 | |
MikamiUitOpen | 3:2649800966bb | 40 | // Initialization by built-in array |
MikamiUitOpen | 1:3df59f65bdcc | 41 | Array<int> xInt(5, (int[]){ 4, 1, -3, 9, -5}); |
MikamiUitOpen | 1:3df59f65bdcc | 42 | for (int n=0; n<xInt.Length(); n++) |
MikamiUitOpen | 1:3df59f65bdcc | 43 | printf("xInt[%d] = %d\r\n", n, xInt[n]); |
MikamiUitOpen | 0:90a30b7b0f2f | 44 | |
MikamiUitOpen | 0:90a30b7b0f2f | 45 | // Test of Matrix class |
MikamiUitOpen | 0:90a30b7b0f2f | 46 | Matrix<int> mat1(3, 2), mat2; |
MikamiUitOpen | 0:90a30b7b0f2f | 47 | for (int n2=0; n2<mat1.Cols(); n2++) |
MikamiUitOpen | 0:90a30b7b0f2f | 48 | for (int n1=0; n1<mat1.Rows(); n1++) |
MikamiUitOpen | 0:90a30b7b0f2f | 49 | mat1[n1][n2] = n2*mat1.Rows() + n1; |
MikamiUitOpen | 0:90a30b7b0f2f | 50 | mat2 = mat1; |
MikamiUitOpen | 0:90a30b7b0f2f | 51 | |
MikamiUitOpen | 0:90a30b7b0f2f | 52 | PrintMatrix("mat2:", mat2); |
MikamiUitOpen | 0:90a30b7b0f2f | 53 | |
MikamiUitOpen | 0:90a30b7b0f2f | 54 | Matrix<int> mat3(5, 2, 10); |
MikamiUitOpen | 0:90a30b7b0f2f | 55 | PrintMatrix("mat3: Initialized valeus", mat3); |
MikamiUitOpen | 0:90a30b7b0f2f | 56 | |
MikamiUitOpen | 0:90a30b7b0f2f | 57 | mat3.Fill(5); |
MikamiUitOpen | 0:90a30b7b0f2f | 58 | PrintMatrix("mat3: Updated valeus", mat3); |
MikamiUitOpen | 0:90a30b7b0f2f | 59 | |
MikamiUitOpen | 0:90a30b7b0f2f | 60 | while (true) {} |
MikamiUitOpen | 0:90a30b7b0f2f | 61 | } |
MikamiUitOpen | 0:90a30b7b0f2f | 62 | |
MikamiUitOpen | 0:90a30b7b0f2f | 63 | void Square(Array<float> & x) |
MikamiUitOpen | 0:90a30b7b0f2f | 64 | { for (int n=0; n<x.Length(); n++) x[n] = x[n]*x[n]; } |
MikamiUitOpen | 0:90a30b7b0f2f | 65 | |
MikamiUitOpen | 0:90a30b7b0f2f | 66 | void Sqrt(float x[], int N) |
MikamiUitOpen | 0:90a30b7b0f2f | 67 | { for (int n=0; n<N; n++) x[n] = sqrtf(x[n]); } |
MikamiUitOpen | 0:90a30b7b0f2f | 68 | |
MikamiUitOpen | 0:90a30b7b0f2f | 69 | void PrintMatrix(const char str[], const Matrix<int> &x) |
MikamiUitOpen | 0:90a30b7b0f2f | 70 | { |
MikamiUitOpen | 0:90a30b7b0f2f | 71 | printf("%s\r\n", str); |
MikamiUitOpen | 0:90a30b7b0f2f | 72 | for (int n2=0; n2<x.Cols(); n2++) |
MikamiUitOpen | 0:90a30b7b0f2f | 73 | { |
MikamiUitOpen | 0:90a30b7b0f2f | 74 | for (int n1=0; n1<x.Rows(); n1++) |
MikamiUitOpen | 0:90a30b7b0f2f | 75 | printf("%3d", x[n1][n2]); |
MikamiUitOpen | 0:90a30b7b0f2f | 76 | printf("\r\n"); |
MikamiUitOpen | 0:90a30b7b0f2f | 77 | } |
MikamiUitOpen | 3:2649800966bb | 78 | } |