Documentation of CSL
vector.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
21 #ifndef VECTOR_H_INCLUDED
22 #define VECTOR_H_INCLUDED
23 
24 #include "abstract.h"
25 #include "literal.h"
26 #include "numerical.h"
27 
28 namespace csl {
29 
31 
32  protected:
33 
34  int dim;
35 
36  int nArgs;
37 
38  csl::vector_expr argument;
39 
40  std::vector<int> shape;
41 
42  public:
43 
45 
46  ~AbstractVectorial(){};
47 
48  std::string const &getName() const override;
49 
50  void print(int mode=0,
51  std::ostream& out=std::cout,
52  bool lib = false) const override;
53 
54  void printCode(
55  int mode = 0,
56  std::ostream &out = std::cout
57  ) const override;
58 
59  std::string printLaTeX(int mode=0) const override;
60 
61  std::vector<Parent> getSubSymbols() const override;
62 
63  std::optional<Expr> evaluate(
64  csl::eval::mode user_mode = csl::eval::base
65  ) const override;
66 
67  unique_Expr copy_unique() const override;
68 
69  Expr deepCopy() const override;
70 
71  Expr refresh() const override;
72 
73  Expr deepRefresh() const override;
74 
75  int getDim() const override { return dim;}
76 
77  csl::PrimaryType getPrimaryType() const override{
79  }
80 
81  int getNArgs(int axis=0) const override;
82 
83  size_t size() const override;
84 
85  csl::vector_expr::iterator begin() override;
86  csl::vector_expr::const_iterator begin() const override
87  {
88  return argument.begin();
89  }
90 
91  csl::vector_expr::iterator end() override;
92  csl::vector_expr::const_iterator end() const override
93  {
94  return argument.end();
95  }
96 
97  bool isReal() const override;
98 
99  bool isPurelyImaginary() const override;
100 
101  std::optional<Expr> getComplexConjugate() const override;
102 
103  std::optional<Expr> getRealPart() const override;
104 
105  Expr getImaginaryPart() const override;
106 
107  std::optional<Expr> getComplexModulus() const override;
108 
109  Expr const &getArgument(int iArg=0) const override;
110 
111  Expr& getArgument(int iArg=0) override;
112 
113  Expr& getArgument(const std::vector<int>& indices) override;
114 
115  Expr const &getArgument(const std::vector<int>& indices) const override;
116 
117  bool dependsOn(Expr_info expr) const override;
118 
119  bool dependsExplicitlyOn(Expr_info expr) const override;
120 
121  bool dependsOn(Parent_info expr) const override;
122 
123  bool dependsExplicitlyOn(Parent_info expr) const override;
124 
125  bool commutesWith(Expr_info expr, int sign=-1) const override;
126 
127  const csl::vector_expr& getVectorArgument() const override;
128 
129  std::vector<int> getShape() const override;
130 
131  void setComplexProperty(csl::ComplexProperty t_prop) override;
132 
133  void setArgument(const Expr& expr, int iArg=0) override;
134 
135  void setArgument(const Expr& expr,
136  const std::vector<int>& indices) override;
137 
138  void setVectorArgument(const csl::vector_expr& t_argument) override;
139 
140  bool matchShape(Expr_info expr, bool exact=false) const override;
141 
142  bool exactMatchShape(Expr_info expr) const;
143 
144  Expr getSum() const override;
145 
146  Expr getProduct() const override;
147 
148  Expr getSubVectorial(const std::vector<int>& exceptions) const override;
149 
151  const std::vector<std::vector<int>>& keepIndices) const override;
152 
154  std::vector<std::vector<int>>::const_iterator begin,
155  std::vector<std::vector<int>>::const_iterator end) const override;
156 
157  Expr getVectorialModulus() const override;
158 
159  Expr dot(const Expr& expr) const override;
160 
161  Expr addition_own(const Expr& expr) const override;
162 
163  Expr multiplication_own(const Expr& expr, bool side=1) const override;
164 
165  Expr tensordot(const Expr& expr) const override;
166 
167  Expr trace(int axis1, int axis2) const override;
168 
169  Expr symmetrise() const override;
170 
171  Expr antisymmetrise() const override;
172 
173  std::optional<Expr> expand(bool full = false,
174  bool inplace = false) const override;
175 
176  std::optional<Expr> expand_if(
177  std::function<bool(Expr const&)> const& f,
178  bool full = false,
179  bool inplace = false) const override;
180 
181  std::optional<Expr> factor(bool full = false) const override;
182 
183  std::optional<Expr> factor(Expr_info expr,
184  bool full = false) const override;
185 
186  bool operator==(Expr_info expr) const override;
187 
188  Expr& operator[](int iArg) override;
189 
190  Expr const &operator[](int iArg) const override;
191 };
192 
193 class Vector: public AbstractVectorial{
194 
195  public:
196 
197  Vector();
198 
199  explicit Vector(int t_nElements);
200 
201  Vector(int t_nElements, const Expr& expr, const Expr& index=CSL_0);
202 
203  explicit Vector(const csl::vector_expr& t_argument);
204 
205  ~Vector(){};
206 
207  csl::Type getType() const override { return csl::Type::Vector;}
208 
210  Expr getSubVectorial(int iExcept) const override;
211 
212  void setElementary(bool t_elementary) override;
213 
214  bool operator<(const Abstract* expr) const override;
215 };
216 
217 Expr vector_s(int t_nElements);
218 
219 Expr vector_s(int t_nElements,const Expr& expr);
220 
221 Expr vector_s(int t_nElements,const Expr& expr, const Expr& index);
222 
223 Expr vector_s(const csl::vector_expr& t_argument);
224 
225 Expr vector_s(const std::initializer_list<Expr>& t_argument);
226 
227 class Matrix: public AbstractVectorial{
228 
229  public:
230 
231  Matrix();
232 
233  explicit Matrix(int t_nArgs);
234 
235  Matrix(int t_x_nArgs, int t_y_nArgs);
236 
237  Matrix(int t_x_nArgs, int t_y_nArgs,
238  const Expr& expr,
239  const Expr& index_x=CSL_0, const Expr& index_y=CSL_0);
240 
241  explicit Matrix(const csl::vector_expr& t_argument);
242 
243  explicit Matrix(const std::vector<csl::vector_expr>& t_argument);
244 
245  ~Matrix(){};
246 
247  csl::Type getType() const override {
248  return csl::Type::Matrix;
249  }
250 
251  Expr determinant() const override;
252 
254  Expr trace() const override;
255 
256  Expr transpose() const override;
257 
258  Expr transposedCoMatrix() const;
259 
260  Expr hermitian() const override;
261 
262  Expr inverseMatrix() const override;
263 
264  Expr symmetrise() const override;
265 
266  Expr antisymmetrise() const override;
267 
269  Expr getSubVectorial(int iExcept, int jExcept) const override;
270 
271  bool operator<(const Abstract* expr) const override;
272 };
273 
274 Expr matrix_s(int t_nArgs);
275 
276 Expr matrix_s(int t_x_nArgs, int t_y_nArgs);
277 
278 Expr matrix_s(int t_x_nArgs, int t_y_nArgs,
279  const Expr& expr,
280  const Expr& index_x, const Expr& index_y);
281 
282 Expr matrix_s(int t_x_nArgs, int t_y_nArgs,
283  const Expr& expr);
284 
285 Expr matrix_s(const csl::vector_expr& t_argument);
286 
287 Expr matrix_s(const std::initializer_list<std::initializer_list<Expr> >&
288  t_matrix);
289 
290 Expr diagonal_s(const csl::vector_expr& diag);
291 
292 Expr identity_s(int dim);
293 
295 
296  public:
297 
298  HighDTensor();
299 
300  explicit HighDTensor(const std::vector<int>& shape);
301 
302  explicit HighDTensor(const std::vector<int>& shape,
303  const Expr& filler);
304 
305  explicit HighDTensor(const csl::vector_expr& t_argument);
306 
307  ~HighDTensor(){};
308 
309  csl::Type getType() const override { return csl::Type::HighDTensor;}
310 
311  bool operator<(const Abstract* expr) const override;
312 };
313 
314 Expr highdtensor_s(const std::vector<int>& shape);
315 
316 Expr highdtensor_s(const std::vector<int>& shape, const Expr& filler);
317 
318 Expr highdtensor_s( const std::initializer_list<std::initializer_list<
319  std::initializer_list<Expr> > >& t_tensor);
320 
321 Expr highdtensor_from_args_s(const std::vector<Expr>& args);
322 
323 Expr vectorialtensor_s(const std::vector<int>& shape);
324 
325 Expr vectorialtensor_s(const std::vector<int>& shape, const Expr& filler);
326 } // End of namespace csl
327 
328 #endif
Expr getSubVectorial(const std::vector< int > &exceptions) const override
Allows to pick a part of a Vectorial expression, excluding the iExcept^{th} element.
Expr const & getArgument(int iArg=0) const override
Definition: vector.cpp:336
Expr symmetrise() const override
Calculates and returns the symmetrization of a 2D matrix.
Definition: vector.cpp:881
virtual bool operator<(Expr_info expr) const =0
Compares the simplicity of the expression to another.
std::optional< Expr > expand(bool full=false, bool inplace=false) const override
Develops the Abstract.
Definition: vector.cpp:950
Namespace for csl library.
Definition: abreviation.h:34
Expr dot(const Expr &expr) const override
Returns the dot product of two Vectorial expressions.
Definition: vector.cpp:627
bool matchShape(Expr_info expr, bool exact=false) const override
In the case of a vectorial-type expression, this function checks if the shape of expr matches itself...
Definition: vector.cpp:487
csl::vector_expr::iterator end() override
Definition: vector.cpp:282
std::optional< Expr > getComplexModulus() const override
Evaluates the modulus in the complex plane of the Abstract and returns it.
Definition: vector.cpp:327
virtual Expr inverseMatrix() const
Calculates and returns the inverse of a 2D square matrix. The applied method is: A^{-1} = 1/det(A)*Co...
Definition: abstract.cpp:1194
Expr tensordot(const Expr &expr) const override
Returns the tensordot of two Vectorial expressions.
Definition: vector.cpp:776
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47
virtual Expr transpose() const
Calculates and returns the transpose of a 2D matrix.
Definition: abstract.cpp:1166
bool dependsExplicitlyOn(Expr_info expr) const override
Check recursively if expr is present in the expression.
Definition: vector.cpp:397
std::optional< Expr > expand_if(std::function< bool(Expr const &)> const &f, bool full=false, bool inplace=false) const override
Develops the Abstract.
Definition: vector.cpp:961
std::string const & getName() const override
Returns the Abstract&#39;s name.
Definition: vector.cpp:44
Definition: vector.h:30
Definition: vector.h:294
virtual Expr trace() const
Definition: abstract.cpp:1152
bool operator==(Expr_info expr) const override
Compares the Abstract with another.
Definition: vector.cpp:995
Expr getProduct() const override
Calculates and returns the product of all elements in the Vectorial object.
Definition: vector.cpp:529
csl::Type getType() const override
Gives the type of Abstract.
Definition: vector.h:309
Expr getVectorialModulus() const override
Returns the Vectorial modulus of the Vectorial object, that is defined here as the squared root of th...
Definition: vector.cpp:610
std::optional< Expr > factor(bool full=false) const override
Factors the Abstract.
Definition: vector.cpp:974
Expr getImaginaryPart() const override
Evaluates the imaginary part of the Abstract and returns it.
Definition: vector.cpp:319
ComplexProperty
Contains all possible complex properties of objects. Real, purely imaginary, or complex.
Definition: enum.h:127
Expr & operator[](int iArg) override
Access operator for multi-argument expressions, returns a reference so this function is not const...
Definition: vector.cpp:1018
csl::Type getType() const override
Gives the type of Abstract.
Definition: vector.h:207
PrimaryType
Stores enumerations of types for Abstract objects.
Definition: enum.h:31
csl::PrimaryType getPrimaryType() const override
Gives the primary type of Abstract.
Definition: vector.h:77
csl::Type getType() const override
Gives the type of Abstract.
Definition: vector.h:247
std::optional< Expr > evaluate(csl::eval::mode user_mode=csl::eval::base) const override
Evaluates the Abstract.
Definition: vector.cpp:157
int getNArgs(int axis=0) const override
Returns the number of arguments of the expression. If the expression is a building block (AbstractBui...
Definition: vector.cpp:258
Definition: vector.h:193
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
std::optional< Expr > getRealPart() const override
Evaluates the real part of the Abstract and returns it.
Definition: vector.cpp:311
Expr getSum() const override
Calculates and returns the sum of all elements in the Vectorial object.
Definition: vector.cpp:516
const csl::vector_expr & getVectorArgument() const override
Allows to get the entire std::vector of arguments of the expression.
Definition: vector.cpp:431
Expr multiplication_own(const Expr &expr, bool side=1) const override
Contains implementation of special multiplication for Numerical- and Vectorial-types.
Definition: vector.cpp:708
Base class for all parents (indicial, fields etc). All parents derive from this class.
Definition: parent.h:81
Expr addition_own(const Expr &expr) const override
Contains implementation of special addition for Numerical- and Vectorial-types.
Definition: vector.cpp:672
std::string printLaTeX(int mode=0) const override
Creates a LaTeX output for the Abstract.
Definition: vector.cpp:85
virtual Expr determinant() const
Returns the determinant of the object if it corresponds to a square matrix (or a scalar), 0 else.
Definition: abstract.cpp:1145
void print(int mode=0, std::ostream &out=std::cout, bool lib=false) const override
Displays the abstract in standard output.
Definition: vector.cpp:49
Base classes for all exprs in the program.
int getDim() const override
Gives the dimension of the object.
Definition: vector.h:75
void setArgument(const Expr &expr, int iArg=0) override
Sets the argument at position iArg (default=0).
Definition: vector.cpp:447
csl::vector_expr::iterator begin() override
Definition: vector.cpp:277
void setVectorArgument(const csl::vector_expr &t_argument) override
Replaced the entire std::vector of argument.
Definition: vector.cpp:455
Expr antisymmetrise() const override
Calculates and returns the anti-symmetrization of a 2D matrix.
Definition: vector.cpp:923
bool commutesWith(Expr_info expr, int sign=-1) const override
Tells if the object commutes with expr.
Definition: vector.cpp:423
std::vector< int > getShape() const override
Accessor to the shape of the tensor in the form of a std::vector of integers.
Definition: vector.cpp:436
Definition: vector.h:227
csl::vector_expr::const_iterator end() const override
Definition: vector.h:92
std::optional< Expr > getComplexConjugate() const override
Calculates and returns the complex conjugate of the expression.
Definition: vector.cpp:303
bool dependsOn(Expr_info expr) const override
Check recursively if the expression depends on expr.
Definition: vector.cpp:385
csl::vector_expr::const_iterator begin() const override
Definition: vector.h:86
Expression type/.
Definition: abstract.h:1573