1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
29 #include <AD/numeric/matrix.h>
31 ////////////////////////////////////////////////////////////////////////////
33 ////////////////////////////////////////////////////////////////////////////
34 static void error_handler(const char format
[], ...)
36 va_start(args
,format
);
37 fprintf (stderr
, "[ Matrix error: ");
38 vfprintf (stderr
, format
, args
);
39 fprintf (stderr
, "]\n");
44 void (*Matrix::error
)(const char [], ...) = error_handler
;
46 ////////////////////////////////////////////////////////////////////////////
48 ////////////////////////////////////////////////////////////////////////////
49 void Matrix::can_add(const Matrix
& m
) const
50 { if (m
.rows() != rows() || m
.columns() != columns())
52 "Can't add/subtract/compare matrices with different dimensions [%d,%d], [%d,%d]",
53 rows(), columns(), m
.rows(), m
.columns());
57 void Matrix::can_mult(const Matrix
& m
) const
58 { if (columns() != m
.rows())
60 "Can't multiply matrices with incompatible dimensions [%d,%d], [%d,%d]",
61 rows(), columns(), m
.rows(), m
.columns());
64 ////////////////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////////////////
67 Matrix
Matrix::operator - ()
69 for (int i
= len
- 1; i
>= 0; i
--) r
.array
[i
] = -array
[i
]; return r
;
72 ////////////////////////////////////////////////////////////////////////////
73 // Matrix operations with scalars
74 ////////////////////////////////////////////////////////////////////////////
75 Matrix
operator + (double a
, const Matrix
& m
)
77 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = a
+ m
.array
[i
];
80 Matrix
operator + (const Matrix
& m
, double a
)
82 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = m
.array
[i
] + a
;
85 Matrix
operator - (double a
, const Matrix
& m
)
87 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = a
- m
.array
[i
];
90 Matrix
operator - (const Matrix
& m
, double a
)
92 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = m
.array
[i
] - a
;
95 Matrix
operator * (double a
, const Matrix
& m
)
97 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = a
* m
.array
[i
];
100 Matrix
operator * (const Matrix
& m
, double a
)
102 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = m
.array
[i
] * a
;
105 Matrix
operator / (const Matrix
& m
, double a
)
107 for (int i
= m
.len
- 1; i
>= 0; i
--) r
.array
[i
] = m
.array
[i
] / a
;
111 //////////////////////////////////////////////////////////////////////////
112 // In place operations with scalars
113 //////////////////////////////////////////////////////////////////////////
114 Matrix
& Matrix::negate()
115 { for (int i
= len
- 1; i
>= 0; i
--) array
[i
] = -array
[i
]; return *this; }
116 Matrix
& Matrix::operator += (double a
)
117 { for (int i
= len
- 1; i
>= 0; i
--) array
[i
] += a
; return *this; }
118 Matrix
& Matrix::operator -= (double a
)
119 { for (int i
= len
- 1; i
>= 0; i
--) array
[i
] -= a
; return *this; }
120 Matrix
& Matrix::operator *= (double a
)
121 { for (int i
= len
- 1; i
>= 0; i
--) array
[i
] *= a
; return *this; }
122 Matrix
& Matrix::operator /= (double a
)
123 { for (int i
= len
- 1; i
>= 0; i
--) array
[i
] /= a
; return *this; }
125 //////////////////////////////////////////////////////////////////////////
126 // Matrix/Matrix operations
127 //////////////////////////////////////////////////////////////////////////
128 Matrix
operator + (const Matrix
& a
, const Matrix
& b
)
131 for (int i
= a
.len
- 1; i
>= 0; i
--) r
.array
[i
] = a
.array
[i
] + b
.array
[i
];
135 Matrix
operator - (const Matrix
& a
, const Matrix
& b
)
138 for (int i
= a
.len
- 1; i
>= 0; i
--) r
.array
[i
] = a
.array
[i
] - b
.array
[i
];
142 Matrix
operator * (const Matrix
& a
, const Matrix
& b
)
145 for (int i
= 0; i
< a
.N
; i
++) {
148 for (j
= 0; j
< b
.M
; j
++)
149 for (int k
= 0; k
< a
.M
; k
++)
150 sum
+= a
[i
][k
] * b
[k
][j
];
156 //////////////////////////////////////////////////////////////////////////
157 // In place matrix operations
158 //////////////////////////////////////////////////////////////////////////
159 double Matrix::det() const
164 Matrix
Matrix::inverse() const
169 Matrix
& Matrix::operator += (const Matrix
& m
)
171 for (int i
= len
- 1; i
>= 0; i
--) array
[i
] += m
.array
[i
];
175 Matrix
& Matrix::operator -= (const Matrix
& m
)
177 for (int i
= len
- 1; i
>= 0; i
--) array
[i
] -= m
.array
[i
]; return *this;
180 Matrix
& Matrix::operator *= (const Matrix
& m
)
181 { *this = *this * m
; return *this; }
183 ////////////////////////////////////////////////////////////////////////////
185 ////////////////////////////////////////////////////////////////////////////
186 Bool
operator == (const Matrix
& a
, const Matrix
& b
)
188 for (int i
= a
.len
- 1; i
>= 0; i
--)
189 if (a
.array
[i
] != b
.array
[i
]) return false;