Qucs-core  0.0.19
matvec.cpp
Go to the documentation of this file.
00001 /*
00002  * matvec.cpp - matrix vector class implementation
00003  *
00004  * Copyright (C) 2004-2009 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <assert.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <cmath>
00034 
00035 #include "logging.h"
00036 #include "object.h"
00037 #include "complex.h"
00038 #include "vector.h"
00039 #include "matrix.h"
00040 #include "matvec.h"
00041 
00042 #if !HAVE_STRCHR
00043 # define strchr  index
00044 # define strrchr rindex
00045 #endif
00046 
00047 namespace qucs {
00048 
00049 // Constructor creates an unnamed instance of the matvec class.
00050 matvec::matvec () {
00051   size = 0;
00052   rows = cols = 0;
00053   name = NULL;
00054   data = NULL;
00055 }
00056 
00057 /* Constructor creates an unnamed instance of the matvec class with a
00058    certain number of empty matrices. */
00059 matvec::matvec (int length, int r, int c) {
00060   size = length;
00061   rows = r;
00062   cols = c;
00063   name = NULL;
00064   if (size > 0) {
00065     data = new matrix[size];
00066     for (int i = 0; i < size; i++) data[i] = matrix (r, c);
00067   } else {
00068     data = NULL;
00069   }
00070 }
00071 
00072 /* The copy constructor creates a new instance based on the given
00073    matvec object. */
00074 matvec::matvec (const matvec & m) {
00075   size = m.size;
00076   rows = m.rows;
00077   cols = m.cols;
00078   name = m.name ? strdup (m.name) : NULL;
00079   data = NULL;
00080 
00081   // copy matvec elements
00082   if (size > 0) {
00083     data = new matrix[size];
00084     for (int i = 0; i < size; i++) data[i] = m.data[i];
00085   }
00086 }
00087 
00088 // Destructor deletes a matvec object.
00089 matvec::~matvec () {
00090   free (name);
00091   delete[] data;
00092 }
00093 
00094 // Sets the name of the matvec object.
00095 void matvec::setName (const char * n) {
00096   free (name);
00097   name = n ? strdup (n) : NULL;
00098 }
00099 
00100 // Returns the name of the matvec object.
00101 char * matvec::getName (void) {
00102   return name;
00103 }
00104 
00105 /* This function saves the given vector to the matvec object with the
00106    appropriate matrix indices. */
00107 void matvec::set (qucs::vector v, int r, int c) {
00108   assert (v.getSize () == size &&
00109           r >= 0 && r < rows && c >= 0 && c < cols);
00110   for (int i = 0; i < size; i++) data[i].set (r, c, v.get (i));
00111 }
00112 
00113 /* The function returns the vector specified by the given matrix
00114    indices.  If the matrix vector has a valid name 'A' the returned
00115    vector gets the name 'A[r,c]'. */
00116 qucs::vector matvec::get (int r, int c) {
00117   assert (r >= 0 && r < rows && c >= 0 && c < cols);
00118   qucs::vector res;
00119   for (int i = 0; i < size; i++) res.add (data[i].get (r, c));
00120   if (name != NULL) {
00121     res.setName (createMatrixString (name, r, c));
00122   }
00123   return res;
00124 }
00125 
00126 /* This function returns a static text representation with the
00127    'n[r,c]' scheme indicating a matrix (vector) entry. */
00128 char * matvec::createMatrixString (const char * n, int r, int c) {
00129   static char str[256]; // hopefully enough
00130   sprintf (str, "%s[%d,%d]", n, r + 1, c + 1);
00131   return str;
00132 }
00133 
00134 /* This function also returns a static text representation with the
00135    'n[r,c]' scheme indicating a matrix (vector) entry but with
00136    different arguments. */
00137 char * matvec::createMatrixString (char n, int r, int c) {
00138   static char str[256]; // hopefully enough
00139   sprintf (str, "%c[%d,%d]", n, r + 1, c + 1);
00140   return str;
00141 }
00142 
00143 /* The function investigates the given vectors name.  If this name
00144    matches the 'n[r,c]' pattern it returns the name 'n' and saves the
00145    row and column indices as well.  The caller is responsible to
00146    'free()' the returned string.  If the vectors name does not match
00147    the pattern the function returns NULL. */
00148 char * matvec::isMatrixVector (const char * n, int& r, int& c) {
00149   const char * p; int len;
00150   char *pnew;
00151   if (n == NULL) return NULL;              // nothing todo here
00152   if ((p = strchr (n, '[')) != NULL) {     // find first '['
00153     r = atoi (p + 1) - 1;                  // get first index
00154     if ((p = strchr (p, ',')) != NULL) {   // find the ','
00155       c = atoi (p + 1) - 1;                // get second index
00156       if ((p = strchr (p, ']')) != NULL) { // find trailing ']'
00157         if (p[1] == '\0') {                // identifier must end in ']'
00158           // parse actual identifier
00159           if ((len = strchr (n, '[') - n) > 0) {
00160             pnew = (char *) malloc (len + 1);
00161             memcpy (pnew, n, len);
00162             pnew[len] = '\0';
00163             return pnew;
00164           }
00165         }
00166       }
00167     }
00168   }
00169   return NULL;
00170 }
00171 
00172 /* This function looks through the vector list given in `data' to find
00173    matrix entries specified by `name' and returns the matrix vector
00174    dimensions. */
00175 void matvec::getMatrixVectorSize (qucs::vector * data, char * name,
00176                                   int& rs, int& cs, int& ss) {
00177   qucs::vector * v;
00178   char * n;
00179   const char *vn;
00180   int r, c, s;
00181   rs = cs = ss = -1;
00182   // go through vector list
00183   for (v = data; v != NULL; v = (qucs::vector *) v->getNext ()) {
00184     vn = v->getName ();
00185     // requested matrix name found?
00186     if (strstr (vn, name) == vn) {
00187       if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
00188         if (rs < r) rs = r;
00189         if (cs < c) cs = c;
00190         s = v->getSize ();
00191         if (ss < s) ss = s;
00192         free (n);
00193       }
00194     }
00195   }
00196 }
00197 
00198 /* This function looks through the vector list given in `data' to find
00199    matrix entries specified by `name' and returns a matrix vector
00200    object.  If there are no such matrices the function returns
00201    NULL. */
00202 matvec * matvec::getMatrixVector (qucs::vector * data, char * name) {
00203 
00204   // obtain matrix vector dimensions
00205   int rs, cs, ss;
00206   getMatrixVectorSize (data, name, rs, cs, ss);
00207 
00208   qucs::vector * v;
00209   const char * vn;
00210   char * n;
00211   int r, c;
00212   // valid matrix entries found
00213   if (rs >= 0 && cs >= 0 && ss > 0) {
00214     // create matrix vector
00215     matvec * mv = new matvec (ss, rs + 1, cs + 1);
00216     mv->setName (name);
00217     // go through vector list again and fill in matrix vectors
00218     for (v = data; v; v = (qucs::vector *) v->getNext ()) {
00219       vn = v->getName ();
00220       if (strstr (vn, name) == vn) {
00221         if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
00222           mv->set (*v, r, c);
00223           free (n);
00224         }
00225       }
00226     }
00227     return mv;
00228   }
00229   return NULL;
00230 }
00231 
00232 /* This function saves the given matrix in the matrix vector at the
00233    specified position. */
00234 void matvec::set (matrix m, int idx) {
00235   assert (m.getRows () == rows && m.getCols () == cols &&
00236           idx >= 0 && idx < size);
00237   data[idx] = m;
00238 }
00239 
00240 /* The function returns the matrix stored within the matrix vector at
00241    the given position. */
00242 matrix matvec::get (int idx) {
00243   assert (idx >= 0 && idx < size);
00244   matrix res (data[idx]);
00245   return res;
00246 }
00247 
00248 // Matrix vector addition.
00249 matvec operator + (matvec a, matvec b) {
00250   assert (a.getRows () == b.getRows () && a.getCols () == b.getCols () &&
00251           a.getSize () == b.getSize ());
00252   matvec res (a.getSize (), a.getRows (), a.getCols ());
00253   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b.get (i), i);
00254   return res;
00255 }
00256 
00257 // Matrix vector addition with single matrix.
00258 matvec operator + (matvec a, matrix b) {
00259   assert (a.getRows () == b.getRows () && a.getCols () == b.getCols ());
00260   matvec res (a.getSize (), a.getRows (), a.getCols ());
00261   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b, i);
00262   return res;
00263 }
00264 
00265 // Matrix vector addition with vector.
00266 matvec operator + (matvec a, qucs::vector b) {
00267   assert (a.getSize () == b.getSize ());
00268   matvec res (a.getSize (), a.getRows (), a.getCols ());
00269   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + b.get (i), i);
00270   return res;
00271 }
00272 
00273 // Matrix vector addition with vector in different order.
00274 matvec operator + (qucs::vector b, matvec a) {
00275   return a + b;
00276 }
00277 
00278 // Matrix vector addition with single matrix in different order.
00279 matvec operator + (matrix a, matvec b) {
00280   return b + a;
00281 }
00282 
00283 // Matrix vector scalar addition.
00284 matvec operator + (matvec a, nr_complex_t z) {
00285   matvec res (a.getSize (), a.getRows (), a.getCols ());
00286   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + z, i);
00287   return res;
00288 }
00289 
00290 // Matrix vector scalar addition in different order.
00291 matvec operator + (nr_complex_t z, matvec a) {
00292   matvec res (a.getSize (), a.getRows (), a.getCols ());
00293   for (int i = 0; i < a.getSize (); i++) res.set (z + a.get (i), i);
00294   return res;
00295 }
00296 
00297 // Matrix vector scalar addition.
00298 matvec operator + (matvec a, nr_double_t d) {
00299   matvec res (a.getSize (), a.getRows (), a.getCols ());
00300   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) + d, i);
00301   return res;
00302 }
00303 
00304 // Matrix vector scalar addition in different order.
00305 matvec operator + (nr_double_t d, matvec a) {
00306   matvec res (a.getSize (), a.getRows (), a.getCols ());
00307   for (int i = 0; i < a.getSize (); i++) res.set (d + a.get (i), i);
00308   return res;
00309 }
00310 
00311 // Matrix vector scalar subtraction.
00312 matvec operator - (matvec a, nr_complex_t z) {
00313   matvec res (a.getSize (), a.getRows (), a.getCols ());
00314   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - z, i);
00315   return res;
00316 }
00317 
00318 // Matrix vector scalar subtraction in different order.
00319 matvec operator - (nr_complex_t z, matvec a) {
00320   matvec res (a.getSize (), a.getRows (), a.getCols ());
00321   for (int i = 0; i < a.getSize (); i++) res.set (z - a.get (i), i);
00322   return res;
00323 }
00324 
00325 // Matrix vector scalar subtraction.
00326 matvec operator - (matvec a, nr_double_t d) {
00327   matvec res (a.getSize (), a.getRows (), a.getCols ());
00328   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - d, i);
00329   return res;
00330 }
00331 
00332 // Matrix vector scalar subtraction in different order.
00333 matvec operator - (nr_double_t d, matvec a) {
00334   matvec res (a.getSize (), a.getRows (), a.getCols ());
00335   for (int i = 0; i < a.getSize (); i++) res.set (d - a.get (i), i);
00336   return res;
00337 }
00338 
00339 // Intrinsic matrix vector addition.
00340 matvec matvec::operator += (matvec a) {
00341   assert (a.getRows () == rows && a.getCols () == cols &&
00342           a.getSize () == size);
00343   for (int i = 0; i < size; i++) data[i] = data[i] + a.get (i);
00344   return *this;
00345 }
00346 
00347 // Matrix vector subtraction.
00348 matvec operator - (matvec a, matvec b) {
00349   assert (a.getRows () == b.getRows () && a.getCols () == b.getCols () &&
00350           a.getSize () == b.getSize ());
00351   matvec res (a.getSize (), a.getRows (), a.getCols ());
00352   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - b.get (i), i);
00353   return res;
00354 }
00355 
00356 // Matrix vector subtraction with single matrix.
00357 matvec operator - (matvec a, matrix b) {
00358   assert (a.getRows () == b.getRows () && a.getCols () == b.getCols ());
00359   matvec res (a.getSize (), a.getRows (), a.getCols ());
00360   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) - b, i);
00361   return res;
00362 }
00363 
00364 // Matrix vector subtraction with single matrix in different order.
00365 matvec operator - (matrix a, matvec b) {
00366   return -b + a;
00367 }
00368 
00369 // Matrix vector subtraction with vector.
00370 matvec operator - (matvec a, qucs::vector b) {
00371   return -b + a;
00372 }
00373 
00374 // Matrix vector subtraction with vector in different order.
00375 matvec operator - (qucs::vector b, matvec a) {
00376   return -a + b;
00377 }
00378 
00379 // Unary minus.
00380 matvec matvec::operator - () {
00381   matvec res (getSize (), getRows (), getCols ());
00382   for (int i = 0; i < getSize (); i++) res.set (-data[i], i);
00383   return res;
00384 }
00385 
00386 // Intrinsic matrix vector subtraction.
00387 matvec matvec::operator -= (matvec a) {
00388   assert (a.getRows () == rows && a.getCols () == cols &&
00389           a.getSize () == size);
00390   for (int i = 0; i < a.getSize (); i++) data[i] = data[i] - a.get (i);
00391   return *this;
00392 }
00393 
00394 // Matrix vector scaling.
00395 matvec operator * (matvec a, nr_complex_t z) {
00396   matvec res (a.getSize (), a.getRows (), a.getCols ());
00397   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * z, i);
00398   return res;
00399 }
00400 
00401 // Matrix vector scaling in different order.
00402 matvec operator * (nr_complex_t z, matvec a) {
00403   return a * z;
00404 }
00405 
00406 // Scalar matrix vector scaling.
00407 matvec operator * (matvec a, nr_double_t d) {
00408   matvec res (a.getSize (), a.getRows (), a.getCols ());
00409   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * d, i);
00410   return res;
00411 }
00412 
00413 // Scalar matrix vector scaling in different order.
00414 matvec operator * (nr_double_t d, matvec a) {
00415   return a * d;
00416 }
00417 
00418 // Matrix vector scaling by a second vector.
00419 matvec operator * (matvec a, qucs::vector b) {
00420   assert (a.getSize () == b.getSize ());
00421   matvec res (a.getSize (), a.getRows (), a.getCols ());
00422   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b.get (i), i);
00423   return res;
00424 }
00425 
00426 // Matrix vector scaling by a second vector in different order.
00427 matvec operator * (qucs::vector a, matvec b) {
00428   return b * a;
00429 }
00430 
00431 // Matrix vector scaling.
00432 matvec operator / (matvec a, nr_complex_t z) {
00433   matvec res (a.getSize (), a.getRows (), a.getCols ());
00434   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / z, i);
00435   return res;
00436 }
00437 
00438 // Scalar matrix vector scaling.
00439 matvec operator / (matvec a, nr_double_t d) {
00440   matvec res (a.getSize (), a.getRows (), a.getCols ());
00441   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / d, i);
00442   return res;
00443 }
00444 
00445 // Matrix vector scaling by a second vector.
00446 matvec operator / (matvec a, qucs::vector b) {
00447   assert (a.getSize () == b.getSize ());
00448   matvec res (a.getSize (), a.getRows (), a.getCols ());
00449   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) / b.get (i), i);
00450   return res;
00451 }
00452 
00453 // Matrix vector multiplication.
00454 matvec operator * (matvec a, matvec b) {
00455   assert (a.getCols () == b.getRows () && a.getSize () == b.getSize ());
00456   matvec res (a.getSize (), a.getRows (), b.getCols ());
00457   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b.get (i), i);
00458   return res;
00459 }
00460 
00461 // Matrix vector multiplication with a single matrix.
00462 matvec operator * (matvec a, matrix b) {
00463   assert (a.getCols () == b.getRows ());
00464   matvec res (a.getSize (), a.getRows (), b.getCols ());
00465   for (int i = 0; i < a.getSize (); i++) res.set (a.get (i) * b, i);
00466   return res;
00467 }
00468 
00469 // Matrix vector multiplication with a single matrix in different order.
00470 matvec operator * (matrix a, matvec b) {
00471   return b * a;
00472 }
00473 
00474 // Compute determinants of the given matrix vector.
00475 qucs::vector det (matvec a) {
00476   qucs::vector res (a.getSize ());
00477   for (int i = 0; i < a.getSize (); i++) res.set (det (a.get (i)), i);
00478   return res;
00479 }
00480 
00481 // Compute inverse matrices of the given matrix vector.
00482 matvec inverse (matvec a) {
00483   matvec res (a.getSize (), a.getRows (), a.getCols ());
00484   for (int i = 0; i < a.getSize (); i++) res.set (inverse (a.get (i)), i);
00485   return res;
00486 }
00487 
00488 // Compute inverse matrices of the given matrix vector.
00489 matvec sqr (matvec a) {
00490   return a * a;
00491 }
00492 
00493 // Compute n-th power of the given matrix vector.
00494 matvec pow (matvec a, int n) {
00495   matvec res (a.getSize (), a.getRows (), a.getCols ());
00496   for (int i = 0; i < a.getSize (); i++) res.set (pow (a.get (i), n), i);
00497   return res;
00498 }
00499 
00500 // Compute n-th powers in the vector of the given matrix vector.
00501 matvec pow (matvec a, qucs::vector v) {
00502   assert (a.getSize () == v.getSize ());
00503   matvec res (a.getSize (), a.getRows (), a.getCols ());
00504   for (int i = 0; i < a.getSize (); i++)
00505     res.set (pow (a.get (i), (int) real (v.get (i))), i);
00506   return res;
00507 }
00508 
00509 // Conjugate complex matrix vector.
00510 matvec conj (matvec a) {
00511   matvec res (a.getSize (), a.getRows (), a.getCols ());
00512   for (int i = 0; i < a.getSize (); i++) res.set (conj (a.get (i)), i);
00513   return res;
00514 }
00515 
00516 // Computes magnitude of each matrix vector element.
00517 matvec abs (matvec a) {
00518   matvec res (a.getSize (), a.getRows (), a.getCols ());
00519   for (int i = 0; i < a.getSize (); i++) res.set (abs (a.get (i)), i);
00520   return res;
00521 }
00522 
00523 // Computes magnitude in dB of each matrix vector element.
00524 matvec dB (matvec a) {
00525   matvec res (a.getSize (), a.getRows (), a.getCols ());
00526   for (int i = 0; i < a.getSize (); i++) res.set (dB (a.get (i)), i);
00527   return res;
00528 }
00529 
00530 // Computes the argument of each matrix vector element.
00531 matvec arg (matvec a) {
00532   matvec res (a.getSize (), a.getRows (), a.getCols ());
00533   for (int i = 0; i < a.getSize (); i++) res.set (arg (a.get (i)), i);
00534   return res;
00535 }
00536 
00537 // Real part matrix vector.
00538 matvec real (matvec a) {
00539   matvec res (a.getSize (), a.getRows (), a.getCols ());
00540   for (int i = 0; i < a.getSize (); i++) res.set (real (a.get (i)), i);
00541   return res;
00542 }
00543 
00544 // Real part matrix vector.
00545 matvec imag (matvec a) {
00546   matvec res (a.getSize (), a.getRows (), a.getCols ());
00547   for (int i = 0; i < a.getSize (); i++) res.set (imag (a.get (i)), i);
00548   return res;
00549 }
00550 
00551 /* The function returns the adjoint complex matrix vector.  This is
00552    also called the adjugate or transpose conjugate. */
00553 matvec adjoint (matvec a) {
00554   matvec res (a.getSize (), a.getRows (), a.getCols ());
00555   for (int i = 0; i < a.getSize (); i++) res.set (adjoint (a.get (i)), i);
00556   return res;
00557 }
00558 
00559 // Transpose the matrix vector.
00560 matvec transpose (matvec a) {
00561   matvec res (a.getSize (), a.getCols (), a.getRows ());
00562   for (int i = 0; i < a.getSize (); i++) res.set (transpose (a.get (i)), i);
00563   return res;
00564 }
00565 
00566 /* Convert scattering parameters with the reference impedance 'zref'
00567    to scattering parameters with the reference impedance 'z0'. */
00568 matvec stos (matvec s, qucs::vector zref, qucs::vector z0) {
00569   assert (s.getCols () == s.getRows () &&
00570           s.getCols () == zref.getSize () && s.getCols () == z0.getSize ());
00571   matvec res (s.getSize (), s.getCols (), s.getRows ());
00572   for (int i = 0; i < s.getSize (); i++)
00573     res.set (stos (s.get (i), zref, z0), i);
00574   return res;
00575 }
00576 
00577 matvec stos (matvec s, nr_complex_t zref, nr_complex_t z0) {
00578   int d = s.getRows ();
00579   return stos (s, qucs::vector (d, zref), qucs::vector (d, z0));
00580 }
00581 
00582 matvec stos (matvec s, nr_double_t zref, nr_double_t z0) {
00583   return stos (s, nr_complex_t (zref, 0), nr_complex_t (z0, 0));
00584 }
00585 
00586 matvec stos (matvec s, qucs::vector zref, nr_complex_t z0) {
00587   return stos (s, zref, qucs::vector (zref.getSize (), z0));
00588 }
00589 
00590 matvec stos (matvec s, nr_complex_t zref, qucs::vector z0) {
00591   return stos (s, qucs::vector (z0.getSize (), zref), z0);
00592 }
00593 
00594 // Convert scattering parameters to admittance matrix vector.
00595 matvec stoy (matvec s, qucs::vector z0) {
00596   assert (s.getCols () == s.getRows () && s.getCols () == z0.getSize ());
00597   matvec res (s.getSize (), s.getCols (), s.getRows ());
00598   for (int i = 0; i < s.getSize (); i++) res.set (stoy (s.get (i), z0), i);
00599   return res;
00600 }
00601 
00602 matvec stoy (matvec s, nr_complex_t z0) {
00603   return stoy (s, qucs::vector (s.getCols (), z0));
00604 }
00605 
00606 // Convert admittance matrix to scattering parameter matrix vector.
00607 matvec ytos (matvec y, qucs::vector z0) {
00608   assert (y.getCols () == y.getRows () && y.getCols () == z0.getSize ());
00609   matvec res (y.getSize (), y.getCols (), y.getRows ());
00610   for (int i = 0; i < y.getSize (); i++) res.set (ytos (y.get (i), z0), i);
00611   return res;
00612 }
00613 
00614 matvec ytos (matvec y, nr_complex_t z0) {
00615   return ytos (y, qucs::vector (y.getCols (), z0));
00616 }
00617 
00618 // Convert scattering parameters to impedance matrix vector.
00619 matvec stoz (matvec s, qucs::vector z0) {
00620   assert (s.getCols () == s.getRows () && s.getCols () == z0.getSize ());
00621   matvec res (s.getSize (), s.getCols (), s.getRows ());
00622   for (int i = 0; i < s.getSize (); i++) res.set (stoz (s.get (i), z0), i);
00623   return res;
00624 }
00625 
00626 matvec stoz (matvec s, nr_complex_t z0) {
00627   return stoz (s, qucs::vector (s.getCols (), z0));
00628 }
00629 
00630 // Convert impedance matrix vector scattering parameter matrix vector.
00631 matvec ztos (matvec z, qucs::vector z0) {
00632   assert (z.getCols () == z.getRows () && z.getCols () == z0.getSize ());
00633   matvec res (z.getSize (), z.getCols (), z.getRows ());
00634   for (int i = 0; i < z.getSize (); i++) res.set (ztos (z.get (i), z0), i);
00635   return res;
00636 }
00637 
00638 matvec ztos (matvec z, nr_complex_t z0) {
00639   return ztos (z, qucs::vector (z.getCols (), z0));
00640 }
00641 
00642 // Convert impedance matrix vector to admittance matrix vector.
00643 matvec ztoy (matvec z) {
00644   assert (z.getCols () == z.getRows ());
00645   matvec res (z.getSize (), z.getCols (), z.getRows ());
00646   for (int i = 0; i < z.getSize (); i++) res.set (ztoy (z.get (i)), i);
00647   return res;
00648 }
00649 
00650 // Convert admittance matrix vector to impedance matrix vector.
00651 matvec ytoz (matvec y) {
00652   assert (y.getCols () == y.getRows ());
00653   matvec res (y.getSize (), y.getCols (), y.getRows ());
00654   for (int i = 0; i < y.getSize (); i++) res.set (ytoz (y.get (i)), i);
00655   return res;
00656 }
00657 
00658 /* This function converts 2x2 matrix vectors from any of the matrix
00659    forms Y, Z, H, G and A to any other.  Also converts S<->(A, T, H, Y
00660    and Z) matrix vectors. */
00661 matvec twoport (matvec m, char in, char out) {
00662   assert (m.getCols () >= 2 && m.getRows () >= 2);
00663   matvec res (m.getSize (), 2, 2);
00664   for (int i = 0; i < m.getSize (); i++)
00665     res.set (twoport (m.get (i), in, out), i);
00666   return res;
00667 }
00668 
00669 /* The function returns the Rollet stability factor vector of the
00670    given S-parameter matrix vector. */
00671 qucs::vector rollet (matvec m) {
00672   assert (m.getCols () >= 2 && m.getRows () >= 2);
00673   qucs::vector res (m.getSize ());
00674   for (int i = 0; i < m.getSize (); i++) res.set (rollet (m.get (i)), i);
00675   return res;
00676 }
00677 
00678 /* The function returns the stability measure B1 vector of the given
00679    S-parameter matrix vector. */
00680 qucs::vector b1 (matvec m) {
00681   assert (m.getCols () >= 2 && m.getRows () >= 2);
00682   qucs::vector res (m.getSize ());
00683   for (int i = 0; i < m.getSize (); i++) res.set (b1 (m.get (i)), i);
00684   return res;
00685 }
00686 
00687 matvec rad2deg (matvec a) {
00688   matvec res (a.getSize (), a.getRows (), a.getCols ());
00689   for (int i = 0; i < a.getSize (); i++) res.set (rad2deg (a.get (i)), i);
00690   return res;
00691 }
00692 
00693 matvec deg2rad (matvec a) {
00694   matvec res (a.getSize (), a.getRows (), a.getCols ());
00695   for (int i = 0; i < a.getSize (); i++) res.set (deg2rad (a.get (i)), i);
00696   return res;
00697 }
00698 
00699 } // namespace qucs