Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "SundanceVectorCalculus.hpp"
00043 #include "SundanceDerivative.hpp"
00044 #include "SundanceListExpr.hpp"
00045
00046
00047 namespace Sundance
00048 {
00049
00050 Expr gradient(int dim)
00051 {
00052 Array<Expr> rtn(dim);
00053 for (int i=0; i<dim; i++)
00054 {
00055 rtn[i] = new Derivative(i);
00056 }
00057 return new ListExpr(rtn);
00058 }
00059
00060 Expr div(const Expr& f)
00061 {
00062 Expr rtn = 0.0;
00063 for (int i=0; i<f.size(); i++)
00064 {
00065 Expr d = new Derivative(i);
00066 rtn = rtn + d*f[i];
00067 }
00068 return rtn;
00069 }
00070
00071
00072 Expr cross(const Expr& a, const Expr& b)
00073 {
00074 TEUCHOS_TEST_FOR_EXCEPTION(a.size() != b.size(), std::runtime_error,
00075 "mismatched vector sizes in cross(a,b): a.size()=" << a.size()
00076 << ", b.size()=" << b.size());
00077
00078 TEUCHOS_TEST_FOR_EXCEPTION(a.size() < 2 || a.size() > 3, std::runtime_error,
00079 "cross(a,b) undefined for dim=" << a.size());
00080
00081 if (a.size()==2)
00082 {
00083 return a[0]*b[1] - a[1]*b[0];
00084 }
00085 else
00086 {
00087 return List(
00088 cross(List(a[1],a[2]), List(b[1],b[2])),
00089 -cross(List(a[0],a[2]), List(b[0],b[2])),
00090 cross(List(a[0],a[1]), List(b[0],b[1]))
00091 );
00092 }
00093 }
00094
00095 Expr curl(const Expr& f)
00096 {
00097 Expr del = gradient(f.size());
00098
00099 return cross(del, f);
00100 }
00101
00102 Expr colonProduct(const Expr& A, const Expr& B)
00103 {
00104 int nA = 0;
00105 int nB = 0;
00106 TEUCHOS_TEST_FOR_EXCEPTION(!isSquareMatrix(A, nA), std::runtime_error,
00107 "Colon product expected argument A=" << A << " to be a square matrix");
00108 TEUCHOS_TEST_FOR_EXCEPTION(!isSquareMatrix(B, nB), std::runtime_error,
00109 "Colon product expected argument B=" << B << " to be a square matrix");
00110
00111 TEUCHOS_TEST_FOR_EXCEPTION(nA!=nB, std::runtime_error,
00112 "Colon product expected operands A=" << A << " and B=" << B
00113 << " to have identical sizes");
00114
00115 Expr rtn = 0.0;
00116 for (int i=0; i<nA; i++)
00117 {
00118 for (int j=0; j<nA; j++)
00119 {
00120 rtn = rtn + A[i][j] * B[i][j];
00121 }
00122 }
00123
00124 return rtn;
00125 }
00126
00127 Expr outerProduct(const Expr& A, const Expr& B)
00128 {
00129 int nA = 0;
00130 int nB = 0;
00131 TEUCHOS_TEST_FOR_EXCEPTION(!isVector(A, nA), std::runtime_error,
00132 "Outer product expected argument A=" << A << " to be a vector");
00133 TEUCHOS_TEST_FOR_EXCEPTION(!isVector(B, nB), std::runtime_error,
00134 "Outer product expected argument B=" << B << " to be a vector");
00135
00136 TEUCHOS_TEST_FOR_EXCEPTION(nA!=nB, std::runtime_error,
00137 "Colon product expected operands A=" << A << " and B=" << B
00138 << " to have identical sizes");
00139
00140 Array<Expr> rtn(nA);
00141 for (int i=0; i<nA; i++)
00142 {
00143 rtn[i] = A[i] * B;
00144 }
00145
00146 return new ListExpr(rtn);
00147 }
00148
00149 bool isVector(const Expr& x, int& N)
00150 {
00151 N = 0;
00152 if (x.size() == x.totalSize())
00153 {
00154 N = x.size();
00155 return true;
00156 }
00157 else
00158 {
00159 return false;
00160 }
00161 }
00162
00163
00164 bool isSquareMatrix(const Expr& x, int& N)
00165 {
00166 N = 0;
00167
00168 if (x.size()*x.size() != x.totalSize()) return false;
00169 N = x.size();
00170 for (int i=0; i<N; i++)
00171 {
00172 int M = 0;
00173 if (!isVector(x[i],M)) return false;
00174 if (M != N) return false;
00175 }
00176 return true;
00177 }
00178
00179
00180
00181
00182 }
00183