25 #include "qucs_typedefs.h"
41 template <
class nr_type_t>
50 template <
class nr_type_t>
54 data =
new nr_type_t[s *
s];
55 memset (
data, 0,
sizeof (nr_type_t) * s * s);
62 template <
class nr_type_t>
67 data =
new nr_type_t[r *
c];
68 memset (
data, 0,
sizeof (nr_type_t) * r * c);
75 template <
class nr_type_t>
82 if (rows > 0 && cols > 0) {
83 data =
new nr_type_t[rows * cols];
90 template <
class nr_type_t>
91 const tmatrix<nr_type_t>&
97 if (rows > 0 && cols > 0) {
98 data =
new nr_type_t[rows * cols];
106 template <
class nr_type_t>
112 template <
class nr_type_t>
114 assert (r >= 0 && r < rows && c >= 0 && c < cols);
115 return data[r * cols +
c];
119 template <
class nr_type_t>
121 assert (r >= 0 && r < rows && c >= 0 && c < cols);
122 data[r * cols +
c] = z;
126 template <
class nr_type_t>
128 for (
int i = 0;
i < rows * cols;
i++)
data[
i] = z;
132 template <
class nr_type_t>
134 assert (r >= 0 && r < rows);
136 nr_type_t * dst = res.
getData ();
137 nr_type_t * src = &
data[r * cols];
138 memcpy (dst, src,
sizeof (nr_type_t) * cols);
143 template <
class nr_type_t>
145 assert (r >= 0 && r < rows && v.
getSize () == cols);
146 nr_type_t * dst = &
data[r * cols];
147 nr_type_t * src = v.
getData ();
148 memcpy (dst, src,
sizeof (nr_type_t) * cols);
152 template <
class nr_type_t>
154 assert (c >= 0 && c < cols);
156 nr_type_t * dst = res.
getData ();
157 nr_type_t * src = &
data[
c];
158 for (
int r = 0;
r < rows;
r++, src += cols, dst++) *dst = *src;
163 template <
class nr_type_t>
165 assert (c >= 0 && c < cols && v.
getSize () == rows);
166 nr_type_t * dst = &
data[
c];
167 nr_type_t * src = v.
getData ();
168 for (
int r = 0;
r < rows;
r++, src++, dst += cols) *dst = *src;
172 template <
class nr_type_t>
174 assert (r1 >= 0 && r2 >= 0 && r1 < rows && r2 < rows);
175 nr_type_t *
s =
new nr_type_t[cols];
176 int len =
sizeof (nr_type_t) * cols;
177 memcpy (s, &
data[r1 * cols], len);
178 memcpy (&
data[r1 * cols], &
data[r2 * cols], len);
179 memcpy (&
data[r2 * cols], s, len);
184 template <
class nr_type_t>
186 assert (c1 >= 0 && c2 >= 0 && c1 < cols && c2 < cols);
188 for (
int r = 0;
r < rows * cols;
r += cols) {
196 template <
class nr_type_t>
197 tmatrix<nr_type_t>
inverse (tmatrix<nr_type_t> a) {
198 nr_double_t MaxPivot;
200 tmatrix<nr_type_t> b;
201 tmatrix<nr_type_t> e;
202 int i,
c,
r, pivot,
n = a.getCols ();
205 b = tmatrix<nr_type_t> (a);
206 e = teye<nr_type_t> (
n);
209 for (i = 0; i <
n; i++) {
211 for (MaxPivot = 0, pivot = r = i; r <
n; r++) {
212 if (
abs (b.get (r, i)) > MaxPivot) {
213 MaxPivot =
abs (b.get (r, i));
218 assert (MaxPivot != 0);
220 b.exchangeRows (i, pivot);
221 e.exchangeRows (i, pivot);
226 for (c = 0; c <
n; c++) {
227 b.set (i, c, b.get (i, c) / f);
228 e.set (i, c, e.get (i, c) / f);
232 for (r = 0; r <
n; r++) {
235 for (c = 0; c <
n; c++) {
236 b.set (r, c, b.get (r, c) - f * b.get (i, c));
237 e.set (r, c, e.get (r, c) - f * e.get (i, c));
246 template <
class nr_type_t>
248 tmatrix<nr_type_t> res (n);
249 for (
int r = 0;
r <
n;
r++) res.set (
r,
r, 1);
254 template <
class nr_type_t>
257 nr_type_t * src = a.
getData ();
258 nr_type_t * dst =
data;
259 for (
int i = 0;
i < rows * cols;
i++) *dst++ += *src++;
264 template <
class nr_type_t>
267 nr_type_t * src = a.
getData ();
268 nr_type_t * dst =
data;
269 for (
int i = 0;
i < rows * cols;
i++) *dst++ -= *src++;
274 template <
class nr_type_t>
275 tmatrix<nr_type_t>
operator * (tmatrix<nr_type_t> a, tmatrix<nr_type_t> b) {
276 assert (a.getCols () == b.getRows ());
277 int r,
c,
i,
n = a.getCols ();
279 tmatrix<nr_type_t> res (a.getRows (), b.getCols ());
280 for (r = 0; r < a.getRows (); r++) {
281 for (c = 0; c < b.getCols (); c++) {
282 for (i = 0, z = 0; i <
n; i++) z += a.get (r, i) * b.get (i, c);
290 template <
class nr_type_t>
291 tvector<nr_type_t>
operator * (tmatrix<nr_type_t> a, tvector<nr_type_t> b) {
292 assert (a.getCols () == b.getSize ());
293 int r,
c,
n = a.getCols ();
295 tvector<nr_type_t> res (n);
297 for (r = 0; r <
n; r++) {
298 for (c = 0, z = 0; c <
n; c++) z += a.get (r, c) * b.get (c);
305 template <
class nr_type_t>
306 tvector<nr_type_t>
operator * (tvector<nr_type_t> a, tmatrix<nr_type_t> b) {
307 assert (a.getSize () == b.getRows ());
308 int r,
c,
n = b.getRows ();
310 tvector<nr_type_t> res (n);
312 for (c = 0; c <
n; c++) {
313 for (r = 0, z = 0; r <
n; r++) z += a.get (r) * b.get (r, c);
320 template <
class nr_type_t>
323 for (
int r = 0;
r < getRows ();
r++)
324 for (
int c = 0;
c <
r;
c++) {
326 set (r,
c,
get (
c, r));
332 template <
class nr_type_t>
334 for (
int i = 0;
i < rows * cols;
i++)
335 if (!std::isfinite (
real (
data[
i])))
return 0;
341 template <
class nr_type_t>
343 for (
int r = 0;
r < rows;
r++) {
344 for (
int c = 0;
c < cols;
c++) {
346 fprintf (stderr,
"%+.2e%s", (
double)
real (
get (
r,
c)),
347 c != cols - 1 ?
" " :
"");
349 fprintf (stderr,
"%+.2e%+.2ei%s", (
double)
real (
get (
r,
c)),
350 (
double)
imag (
get (
r,
c)),
c != cols - 1 ?
" " :
"");
353 fprintf (stderr,
";\n");
matrix inverse(matrix a)
Compute inverse matrix.
matrix real(matrix a)
Real part matrix.
matrix abs(matrix a)
Computes magnitude of each matrix element.
matrix operator*(matrix a, nr_complex_t z)
Matrix scaling complex version.
void setRow(int, tvector< nr_type_t >)
void set(int, int, nr_type_t)
nr_type_t * getData(void)
void exchangeRows(int, int)
tmatrix< nr_type_t > teye(int n)
tvector< nr_type_t > getRow(int)
matrix imag(matrix a)
Imaginary part matrix.
void setCol(int, tvector< nr_type_t >)
const tmatrix & operator=(const tmatrix &)
Declaration sizeof(struct vcd_scope))
tmatrix operator-=(tmatrix)
tmatrix operator+=(tmatrix)
void print(bool realonly=false)
tvector< nr_type_t > getCol(int)
void exchangeCols(int, int)
nr_type_t * getData(void)