00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GENLIB2_VECMAT_INCLUDED
00022 #define GENLIB2_VECMAT_INCLUDED
00023
00024 #include <cassert>
00025 #include <iostream>
00026 #include "vector.h"
00027 #include "matrix.h"
00028
00029 using namespace std;
00030
00031 namespace GenLib2
00032 {
00033
00035 template<class T>
00036 void mult(Vector<T>& c, const Matrix<T>& A, const Vector<T>& b, bool clr=true)
00037 {
00038 int m = A.rows();
00039 int n = A.cols();
00040 int o = b.size();
00041
00042
00043 assert(n == o);
00044
00045
00046 c.newsize(m);
00047
00048
00049 if (clr) c.clear();
00050
00051 for (register int i = 0; i < m; i++)
00052 {
00053 register T sum = (T)0;
00054
00055 for (register int j = 0; j < n; j++)
00056 {
00057 sum += A(i,j) * b(j);
00058 }
00059
00060 c(i) += sum;
00061 }
00062 }
00063
00064
00066 template<class T>
00067 Vector<T> mult(const Matrix<T>& A, const Vector<T>& b)
00068 {
00069 Vector<T> c;
00070 mult(c, A, b, true);
00071 return c;
00072 }
00073
00074
00076 template<class T>
00077 Vector<T> operator*(const Matrix<T>& A, const Vector<T>& b)
00078 {
00079 return mult(A,b);
00080 }
00081
00082
00084 template<class T>
00085 void mult(Vector<T>& c, const Vector<T>& a, const Matrix<T>& B, bool clr=true)
00086 {
00087 int n = a.size();
00088 int o = B.rows();
00089 int p = B.cols();
00090
00091
00092 assert(n == o);
00093
00094
00095 c.newsize(p);
00096
00097
00098 if (clr) c.clear();
00099
00100 for (register int i = 0; i < p; i++)
00101 {
00102 register T sum = (T)0;
00103
00104 for (register int j = 0; j < n; j++)
00105 {
00106 sum += a(j) * B(j,i);
00107 }
00108
00109 c(i) += sum;
00110 }
00111 }
00112
00113
00115 template<class T>
00116 Vector<T> mult(const Vector<T>& a, const Matrix<T>& B)
00117 {
00118 Vector<T> c;
00119 mult(c, a, B, true);
00120 return c;
00121 }
00122
00123
00125 template<class T>
00126 Vector<T> operator*(const Vector<T>& a, const Matrix<T>& B)
00127 {
00128 return mult(a,B);
00129 }
00130
00131 }
00132
00133 #endif