/doc/project_plan/tools/qucs-code/qucs-core/src/matvec.cpp

https://bitbucket.org/JonSRomero/arduino_motorcycle_gauges · C++ · 675 lines · 464 code · 85 blank · 126 comment · 150 complexity · c0fc621f162173f482d9e1f436e13a8c MD5 · raw file

  1. /*
  2. * matvec.cpp - matrix vector class implementation
  3. *
  4. * Copyright (C) 2004-2009 Stefan Jahn <stefan@lkcc.org>
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this package; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. * $Id: matvec.cpp 1825 2011-03-11 20:42:14Z ela $
  22. *
  23. */
  24. #if HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <math.h>
  32. #include "logging.h"
  33. #include "object.h"
  34. #include "complex.h"
  35. #include "vector.h"
  36. #include "matrix.h"
  37. #include "matvec.h"
  38. #if !HAVE_STRCHR
  39. # define strchr index
  40. # define strrchr rindex
  41. #endif
  42. // Constructor creates an unnamed instance of the matvec class.
  43. matvec::matvec () {
  44. size = 0;
  45. rows = cols = 0;
  46. name = NULL;
  47. data = NULL;
  48. }
  49. /* Constructor creates an unnamed instance of the matvec class with a
  50. certain number of empty matrices. */
  51. matvec::matvec (int length, int r, int c) {
  52. size = length;
  53. rows = r;
  54. cols = c;
  55. name = NULL;
  56. if (size > 0) {
  57. data = new matrix[size];
  58. for (int i = 0; i < size; i++) data[i] = matrix (r, c);
  59. } else {
  60. data = NULL;
  61. }
  62. }
  63. /* The copy constructor creates a new instance based on the given
  64. matvec object. */
  65. matvec::matvec (const matvec & m) {
  66. size = m.size;
  67. rows = m.rows;
  68. cols = m.cols;
  69. name = m.name ? strdup (m.name) : NULL;
  70. data = NULL;
  71. // copy matvec elements
  72. if (size > 0) {
  73. data = new matrix[size];
  74. for (int i = 0; i < size; i++) data[i] = m.data[i];
  75. }
  76. }
  77. // Destructor deletes a matvec object.
  78. matvec::~matvec () {
  79. if (name) free (name);
  80. if (data) delete[] data;
  81. }
  82. // Sets the name of the matvec object.
  83. void matvec::setName (const char * n) {
  84. if (name) free (name);
  85. name = n ? strdup (n) : NULL;
  86. }
  87. // Returns the name of the matvec object.
  88. char * matvec::getName (void) {
  89. return name;
  90. }
  91. /* This function saves the given vector to the matvec object with the
  92. appropriate matrix indices. */
  93. void matvec::set (vector v, int r, int c) {
  94. assert (v.getSize () == size &&
  95. r >= 0 && r < rows && c >= 0 && c < cols);
  96. for (int i = 0; i < size; i++) data[i].set (r, c, v.get (i));
  97. }
  98. /* The function returns the vector specified by the given matrix
  99. indices. If the matrix vector has a valid name 'A' the returned
  100. vector gets the name 'A[r,c]'. */
  101. vector matvec::get (int r, int c) {
  102. assert (r >= 0 && r < rows && c >= 0 && c < cols);
  103. vector res;
  104. for (int i = 0; i < size; i++) res.add (data[i].get (r, c));
  105. if (name != NULL) {
  106. res.setName (createMatrixString (name, r, c));
  107. }
  108. return res;
  109. }
  110. /* This function returns a static text representation with the
  111. 'n[r,c]' scheme indicating a matrix (vector) entry. */
  112. char * matvec::createMatrixString (const char * n, int r, int c) {
  113. static char str[256]; // hopefully enough
  114. sprintf (str, "%s[%d,%d]", n, r + 1, c + 1);
  115. return str;
  116. }
  117. /* This function also returns a static text representation with the
  118. 'n[r,c]' scheme indicating a matrix (vector) entry but with
  119. different arguments. */
  120. char * matvec::createMatrixString (char n, int r, int c) {
  121. static char str[256]; // hopefully enough
  122. sprintf (str, "%c[%d,%d]", n, r + 1, c + 1);
  123. return str;
  124. }
  125. /* The function investigates the given vectors name. If this name
  126. matches the 'n[r,c]' pattern it returns the name 'n' and saves the
  127. row and column indices as well. The caller is responsible to
  128. 'free()' the returned string. If the vectors name does not match
  129. the pattern the function returns NULL. */
  130. char * matvec::isMatrixVector (char * n, int& r, int& c) {
  131. char * p; int len;
  132. if (n == NULL) return NULL; // nothing todo here
  133. if ((p = strchr (n, '[')) != NULL) { // find first '['
  134. r = atoi (p + 1) - 1; // get first index
  135. if ((p = strchr (p, ',')) != NULL) { // find the ','
  136. c = atoi (p + 1) - 1; // get second index
  137. if ((p = strchr (p, ']')) != NULL) { // find trailing ']'
  138. if (p[1] == '\0') { // identifier must end in ']'
  139. // parse actual identifier
  140. if ((len = strchr (n, '[') - n) > 0) {
  141. p = (char *) malloc (len + 1);
  142. memcpy (p, n, len);
  143. p[len] = '\0';
  144. return p;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return NULL;
  151. }
  152. /* This function looks through the vector list given in `data' to find
  153. matrix entries specified by `name' and returns the matrix vector
  154. dimensions. */
  155. void matvec::getMatrixVectorSize (vector * data, char * name,
  156. int& rs, int& cs, int& ss) {
  157. vector * v;
  158. char * vn, * n;
  159. int r, c, s;
  160. rs = cs = ss = -1;
  161. // go through vector list
  162. for (v = data; v != NULL; v = (vector *) v->getNext ()) {
  163. vn = v->getName ();
  164. // requested matrix name found?
  165. if (strstr (vn, name) == vn) {
  166. if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
  167. if (rs < r) rs = r;
  168. if (cs < c) cs = c;
  169. s = v->getSize ();
  170. if (ss < s) ss = s;
  171. free (n);
  172. }
  173. }
  174. }
  175. }
  176. /* This function looks through the vector list given in `data' to find
  177. matrix entries specified by `name' and returns a matrix vector
  178. object. If there are no such matrices the function returns
  179. NULL. */
  180. matvec * matvec::getMatrixVector (vector * data, char * name) {
  181. // obtain matrix vector dimensions
  182. int rs, cs, ss;
  183. getMatrixVectorSize (data, name, rs, cs, ss);
  184. vector * v;
  185. char * vn, * n;
  186. int r, c;
  187. // valid matrix entries found
  188. if (rs >= 0 && cs >= 0 && ss > 0) {
  189. // create matrix vector
  190. matvec * mv = new matvec (ss, rs + 1, cs + 1);
  191. mv->setName (name);
  192. // go through vector list again and fill in matrix vectors
  193. for (v = data; v; v = (vector *) v->getNext ()) {
  194. vn = v->getName ();
  195. if (strstr (vn, name) == vn) {
  196. if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
  197. mv->set (*v, r, c);
  198. free (n);
  199. }
  200. }
  201. }
  202. return mv;
  203. }
  204. return NULL;
  205. }
  206. /* This function saves the given matrix in the matrix vector at the
  207. specified position. */
  208. void matvec::set (matrix m, int idx) {
  209. assert (m.getRows () == rows && m.getCols () == cols &&
  210. idx >= 0 && idx < size);
  211. data[idx] = m;
  212. }
  213. /* The function returns the matrix stored within the matrix vector at
  214. the given position. */
  215. matrix matvec::get (int idx) {
  216. assert (idx >= 0 && idx < size);
  217. matrix res (data[idx]);
  218. return res;
  219. }
  220. // Matrix vector addition.
  221. matvec operator + (matvec a, matvec b) {
  222. assert (a.getRows () == b.getRows () && a.getCols () == b.getCols () &&
  223. a.getSize () == b.getSize ());
  224. matvec res (a.getSize (), a.getRows (), a.getCols ());
  225. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b.get (i), i);
  226. return res;
  227. }
  228. // Matrix vector addition with single matrix.
  229. matvec operator + (matvec a, matrix b) {
  230. assert (a.getRows () == b.getRows () && a.getCols () == b.getCols ());
  231. matvec res (a.getSize (), a.getRows (), a.getCols ());
  232. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b, i);
  233. return res;
  234. }
  235. // Matrix vector addition with vector.
  236. matvec operator + (matvec a, vector b) {
  237. assert (a.getSize () == b.getSize ());
  238. matvec res (a.getSize (), a.getRows (), a.getCols ());
  239. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b.get (i), i);
  240. return res;
  241. }
  242. // Matrix vector addition with vector in different order.
  243. matvec operator + (vector b, matvec a) {
  244. return a + b;
  245. }
  246. // Matrix vector addition with single matrix in different order.
  247. matvec operator + (matrix a, matvec b) {
  248. return b + a;
  249. }
  250. // Matrix vector scalar addition.
  251. matvec operator + (matvec a, nr_complex_t z) {
  252. matvec res (a.getSize (), a.getRows (), a.getCols ());
  253. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + z, i);
  254. return res;
  255. }
  256. // Matrix vector scalar addition in different order.
  257. matvec operator + (nr_complex_t z, matvec a) {
  258. matvec res (a.getSize (), a.getRows (), a.getCols ());
  259. for (int i = 0; i < a.getSize (); i++) res.set (z + a.get (i), i);
  260. return res;
  261. }
  262. // Matrix vector scalar addition.
  263. matvec operator + (matvec a, nr_double_t d) {
  264. matvec res (a.getSize (), a.getRows (), a.getCols ());
  265. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + d, i);
  266. return res;
  267. }
  268. // Matrix vector scalar addition in different order.
  269. matvec operator + (nr_double_t d, matvec a) {
  270. matvec res (a.getSize (), a.getRows (), a.getCols ());
  271. for (int i = 0; i < a.getSize (); i++) res.set (d + a.get (i), i);
  272. return res;
  273. }
  274. // Matrix vector scalar subtraction.
  275. matvec operator - (matvec a, nr_complex_t z) {
  276. matvec res (a.getSize (), a.getRows (), a.getCols ());
  277. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - z, i);
  278. return res;
  279. }
  280. // Matrix vector scalar subtraction in different order.
  281. matvec operator - (nr_complex_t z, matvec a) {
  282. matvec res (a.getSize (), a.getRows (), a.getCols ());
  283. for (int i = 0; i < a.getSize (); i++) res.set (z - a.get (i), i);
  284. return res;
  285. }
  286. // Matrix vector scalar subtraction.
  287. matvec operator - (matvec a, nr_double_t d) {
  288. matvec res (a.getSize (), a.getRows (), a.getCols ());
  289. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - d, i);
  290. return res;
  291. }
  292. // Matrix vector scalar subtraction in different order.
  293. matvec operator - (nr_double_t d, matvec a) {
  294. matvec res (a.getSize (), a.getRows (), a.getCols ());
  295. for (int i = 0; i < a.getSize (); i++) res.set (d - a.get (i), i);
  296. return res;
  297. }
  298. // Intrinsic matrix vector addition.
  299. matvec matvec::operator += (matvec a) {
  300. assert (a.getRows () == rows && a.getCols () == cols &&
  301. a.getSize () == size);
  302. for (int i = 0; i < size; i++) data[i] += a.get (i);
  303. return *this;
  304. }
  305. // Matrix vector subtraction.
  306. matvec operator - (matvec a, matvec b) {
  307. assert (a.getRows () == b.getRows () && a.getCols () == b.getCols () &&
  308. a.getSize () == b.getSize ());
  309. matvec res (a.getSize (), a.getRows (), a.getCols ());
  310. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - b.get (i), i);
  311. return res;
  312. }
  313. // Matrix vector subtraction with single matrix.
  314. matvec operator - (matvec a, matrix b) {
  315. assert (a.getRows () == b.getRows () && a.getCols () == b.getCols ());
  316. matvec res (a.getSize (), a.getRows (), a.getCols ());
  317. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - b, i);
  318. return res;
  319. }
  320. // Matrix vector subtraction with single matrix in different order.
  321. matvec operator - (matrix a, matvec b) {
  322. return -b + a;
  323. }
  324. // Matrix vector subtraction with vector.
  325. matvec operator - (matvec a, vector b) {
  326. return -b + a;
  327. }
  328. // Matrix vector subtraction with vector in different order.
  329. matvec operator - (vector b, matvec a) {
  330. return -a + b;
  331. }
  332. // Unary minus.
  333. matvec matvec::operator - () {
  334. matvec res (getSize (), getRows (), getCols ());
  335. for (int i = 0; i < getSize (); i++) res.set (-data[i], i);
  336. return res;
  337. }
  338. // Intrinsic matrix vector subtraction.
  339. matvec matvec::operator -= (matvec a) {
  340. assert (a.getRows () == rows && a.getCols () == cols &&
  341. a.getSize () == size);
  342. for (int i = 0; i < a.getSize (); i++) data[i] -= a.get (i);
  343. return *this;
  344. }
  345. // Matrix vector scaling.
  346. matvec operator * (matvec a, nr_complex_t z) {
  347. matvec res (a.getSize (), a.getRows (), a.getCols ());
  348. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * z, i);
  349. return res;
  350. }
  351. // Matrix vector scaling in different order.
  352. matvec operator * (nr_complex_t z, matvec a) {
  353. return a * z;
  354. }
  355. // Scalar matrix vector scaling.
  356. matvec operator * (matvec a, nr_double_t d) {
  357. matvec res (a.getSize (), a.getRows (), a.getCols ());
  358. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * d, i);
  359. return res;
  360. }
  361. // Scalar matrix vector scaling in different order.
  362. matvec operator * (nr_double_t d, matvec a) {
  363. return a * d;
  364. }
  365. // Matrix vector scaling by a second vector.
  366. matvec operator * (matvec a, vector b) {
  367. assert (a.getSize () == b.getSize ());
  368. matvec res (a.getSize (), a.getRows (), a.getCols ());
  369. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b.get (i), i);
  370. return res;
  371. }
  372. // Matrix vector scaling by a second vector in different order.
  373. matvec operator * (vector a, matvec b) {
  374. return b * a;
  375. }
  376. // Matrix vector scaling.
  377. matvec operator / (matvec a, nr_complex_t z) {
  378. matvec res (a.getSize (), a.getRows (), a.getCols ());
  379. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / z, i);
  380. return res;
  381. }
  382. // Scalar matrix vector scaling.
  383. matvec operator / (matvec a, nr_double_t d) {
  384. matvec res (a.getSize (), a.getRows (), a.getCols ());
  385. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / d, i);
  386. return res;
  387. }
  388. // Matrix vector scaling by a second vector.
  389. matvec operator / (matvec a, vector b) {
  390. assert (a.getSize () == b.getSize ());
  391. matvec res (a.getSize (), a.getRows (), a.getCols ());
  392. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / b.get (i), i);
  393. return res;
  394. }
  395. // Matrix vector multiplication.
  396. matvec operator * (matvec a, matvec b) {
  397. assert (a.getCols () == b.getRows () && a.getSize () == b.getSize ());
  398. matvec res (a.getSize (), a.getRows (), b.getCols ());
  399. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b.get (i), i);
  400. return res;
  401. }
  402. // Matrix vector multiplication with a single matrix.
  403. matvec operator * (matvec a, matrix b) {
  404. assert (a.getCols () == b.getRows ());
  405. matvec res (a.getSize (), a.getRows (), b.getCols ());
  406. for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b, i);
  407. return res;
  408. }
  409. // Matrix vector multiplication with a single matrix in different order.
  410. matvec operator * (matrix a, matvec b) {
  411. return b * a;
  412. }
  413. // Compute determinants of the given matrix vector.
  414. vector det (matvec a) {
  415. vector res (a.getSize ());
  416. for (int i = 0; i < a.getSize (); i++) res.set (det (a.get (i)), i);
  417. return res;
  418. }
  419. // Compute inverse matrices of the given matrix vector.
  420. matvec inverse (matvec a) {
  421. matvec res (a.getSize (), a.getRows (), a.getCols ());
  422. for (int i = 0; i < a.getSize (); i++) res.set (inverse (a.get (i)), i);
  423. return res;
  424. }
  425. // Compute n-th power of the given matrix vector.
  426. matvec pow (matvec a, int n) {
  427. matvec res (a.getSize (), a.getRows (), a.getCols ());
  428. for (int i = 0; i < a.getSize (); i++) res.set (pow (a.get (i), n), i);
  429. return res;
  430. }
  431. // Compute n-th powers in the vector of the given matrix vector.
  432. matvec pow (matvec a, vector v) {
  433. assert (a.getSize () == v.getSize ());
  434. matvec res (a.getSize (), a.getRows (), a.getCols ());
  435. for (int i = 0; i < a.getSize (); i++)
  436. res.set (pow (a.get (i), (int) real (v.get (i))), i);
  437. return res;
  438. }
  439. // Conjugate complex matrix vector.
  440. matvec conj (matvec a) {
  441. matvec res (a.getSize (), a.getRows (), a.getCols ());
  442. for (int i = 0; i < a.getSize (); i++) res.set (conj (a.get (i)), i);
  443. return res;
  444. }
  445. // Computes magnitude of each matrix vector element.
  446. matvec abs (matvec a) {
  447. matvec res (a.getSize (), a.getRows (), a.getCols ());
  448. for (int i = 0; i < a.getSize (); i++) res.set (abs (a.get (i)), i);
  449. return res;
  450. }
  451. // Computes magnitude in dB of each matrix vector element.
  452. matvec dB (matvec a) {
  453. matvec res (a.getSize (), a.getRows (), a.getCols ());
  454. for (int i = 0; i < a.getSize (); i++) res.set (dB (a.get (i)), i);
  455. return res;
  456. }
  457. // Computes the argument of each matrix vector element.
  458. matvec arg (matvec a) {
  459. matvec res (a.getSize (), a.getRows (), a.getCols ());
  460. for (int i = 0; i < a.getSize (); i++) res.set (arg (a.get (i)), i);
  461. return res;
  462. }
  463. // Real part matrix vector.
  464. matvec real (matvec a) {
  465. matvec res (a.getSize (), a.getRows (), a.getCols ());
  466. for (int i = 0; i < a.getSize (); i++) res.set (real (a.get (i)), i);
  467. return res;
  468. }
  469. // Real part matrix vector.
  470. matvec imag (matvec a) {
  471. matvec res (a.getSize (), a.getRows (), a.getCols ());
  472. for (int i = 0; i < a.getSize (); i++) res.set (imag (a.get (i)), i);
  473. return res;
  474. }
  475. /* The function returns the adjoint complex matrix vector. This is
  476. also called the adjugate or transpose conjugate. */
  477. matvec adjoint (matvec a) {
  478. matvec res (a.getSize (), a.getRows (), a.getCols ());
  479. for (int i = 0; i < a.getSize (); i++) res.set (adjoint (a.get (i)), i);
  480. return res;
  481. }
  482. // Transpose the matrix vector.
  483. matvec transpose (matvec a) {
  484. matvec res (a.getSize (), a.getCols (), a.getRows ());
  485. for (int i = 0; i < a.getSize (); i++) res.set (transpose (a.get (i)), i);
  486. return res;
  487. }
  488. /* Convert scattering parameters with the reference impedance 'zref'
  489. to scattering parameters with the reference impedance 'z0'. */
  490. matvec stos (matvec s, vector zref, vector z0) {
  491. assert (s.getCols () == s.getRows () &&
  492. s.getCols () == zref.getSize () && s.getCols () == z0.getSize ());
  493. matvec res (s.getSize (), s.getCols (), s.getRows ());
  494. for (int i = 0; i < s.getSize (); i++)
  495. res.set (stos (s.get (i), zref, z0), i);
  496. return res;
  497. }
  498. matvec stos (matvec s, nr_complex_t zref, nr_complex_t z0) {
  499. int d = s.getRows ();
  500. return stos (s, vector (d, zref), vector (d, z0));
  501. }
  502. matvec stos (matvec s, nr_double_t zref, nr_double_t z0) {
  503. return stos (s, rect (zref, 0), rect (z0, 0));
  504. }
  505. matvec stos (matvec s, vector zref, nr_complex_t z0) {
  506. return stos (s, zref, vector (zref.getSize (), z0));
  507. }
  508. matvec stos (matvec s, nr_complex_t zref, vector z0) {
  509. return stos (s, vector (z0.getSize (), zref), z0);
  510. }
  511. // Convert scattering parameters to admittance matrix vector.
  512. matvec stoy (matvec s, vector z0) {
  513. assert (s.getCols () == s.getRows () && s.getCols () == z0.getSize ());
  514. matvec res (s.getSize (), s.getCols (), s.getRows ());
  515. for (int i = 0; i < s.getSize (); i++) res.set (stoy (s.get (i), z0), i);
  516. return res;
  517. }
  518. matvec stoy (matvec s, nr_complex_t z0) {
  519. return stoy (s, vector (s.getCols (), z0));
  520. }
  521. // Convert admittance matrix to scattering parameter matrix vector.
  522. matvec ytos (matvec y, vector z0) {
  523. assert (y.getCols () == y.getRows () && y.getCols () == z0.getSize ());
  524. matvec res (y.getSize (), y.getCols (), y.getRows ());
  525. for (int i = 0; i < y.getSize (); i++) res.set (ytos (y.get (i), z0), i);
  526. return res;
  527. }
  528. matvec ytos (matvec y, nr_complex_t z0) {
  529. return ytos (y, vector (y.getCols (), z0));
  530. }
  531. // Convert scattering parameters to impedance matrix vector.
  532. matvec stoz (matvec s, vector z0) {
  533. assert (s.getCols () == s.getRows () && s.getCols () == z0.getSize ());
  534. matvec res (s.getSize (), s.getCols (), s.getRows ());
  535. for (int i = 0; i < s.getSize (); i++) res.set (stoz (s.get (i), z0), i);
  536. return res;
  537. }
  538. matvec stoz (matvec s, nr_complex_t z0) {
  539. return stoz (s, vector (s.getCols (), z0));
  540. }
  541. // Convert impedance matrix vector scattering parameter matrix vector.
  542. matvec ztos (matvec z, vector z0) {
  543. assert (z.getCols () == z.getRows () && z.getCols () == z0.getSize ());
  544. matvec res (z.getSize (), z.getCols (), z.getRows ());
  545. for (int i = 0; i < z.getSize (); i++) res.set (ztos (z.get (i), z0), i);
  546. return res;
  547. }
  548. matvec ztos (matvec z, nr_complex_t z0) {
  549. return ztos (z, vector (z.getCols (), z0));
  550. }
  551. // Convert impedance matrix vector to admittance matrix vector.
  552. matvec ztoy (matvec z) {
  553. assert (z.getCols () == z.getRows ());
  554. matvec res (z.getSize (), z.getCols (), z.getRows ());
  555. for (int i = 0; i < z.getSize (); i++) res.set (ztoy (z.get (i)), i);
  556. return res;
  557. }
  558. // Convert admittance matrix vector to impedance matrix vector.
  559. matvec ytoz (matvec y) {
  560. assert (y.getCols () == y.getRows ());
  561. matvec res (y.getSize (), y.getCols (), y.getRows ());
  562. for (int i = 0; i < y.getSize (); i++) res.set (ytoz (y.get (i)), i);
  563. return res;
  564. }
  565. /* This function converts 2x2 matrix vectors from any of the matrix
  566. forms Y, Z, H, G and A to any other. Also converts S<->(A, T, H, Y
  567. and Z) matrix vectors. */
  568. matvec twoport (matvec m, char in, char out) {
  569. assert (m.getCols () >= 2 && m.getRows () >= 2);
  570. matvec res (m.getSize (), 2, 2);
  571. for (int i = 0; i < m.getSize (); i++)
  572. res.set (twoport (m.get (i), in, out), i);
  573. return res;
  574. }
  575. /* The function returns the Rollet stability factor vector of the
  576. given S-parameter matrix vector. */
  577. vector rollet (matvec m) {
  578. assert (m.getCols () >= 2 && m.getRows () >= 2);
  579. vector res (m.getSize ());
  580. for (int i = 0; i < m.getSize (); i++) res.set (rollet (m.get (i)), i);
  581. return res;
  582. }
  583. /* The function returns the stability measure B1 vector of the given
  584. S-parameter matrix vector. */
  585. vector b1 (matvec m) {
  586. assert (m.getCols () >= 2 && m.getRows () >= 2);
  587. vector res (m.getSize ());
  588. for (int i = 0; i < m.getSize (); i++) res.set (b1 (m.get (i)), i);
  589. return res;
  590. }