xc
nDarray.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // XC program; finite element analysis code
4 // for structural analysis and design.
5 //
6 // Copyright (C) Luis C. Pérez Tato
7 //
8 // This program derives from OpenSees <http://opensees.berkeley.edu>
9 // developed by the «Pacific earthquake engineering research center».
10 //
11 // Except for the restrictions that may arise from the copyright
12 // of the original program (see copyright_opensees.txt)
13 // XC is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // This software is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program.
26 // If not, see <http://www.gnu.org/licenses/>.
27 //----------------------------------------------------------------------------
28 //#############################################################################
29 // #
30 // #
31 // /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/~~\ #
32 // | |____| #
33 // | | #
34 // | | #
35 // | | #
36 // | | #
37 // | B A S E C L A S S E S | #
38 // | | #
39 // | | #
40 // | | #
41 // | | #
42 // | C + + H E A D E R | #
43 // | | #
44 // | | #
45 // | | #
46 // | | #
47 // /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/ | #
48 // \_________________________________________\__/ #
49 // #
50 // #
51 //#############################################################################
52 //#############################################################################
54 //################################################################################
55 //# COPYRIGHT (C): :-)) #
56 //# PROJECT: Object Oriented Finite Element Program #
57 //# PURPOSE: #
58 //# CLASS: nDarray #
59 //# #
60 //# VERSION: #
61 //# LANGUAGE: C++.ver >= 2.0 ( Borland C++ ver=3.10, SUN C++ ver=2.1 ) #
62 //# TARGET OS: DOS || UNIX || . . . #
63 //# DESIGNER(S): Boris Jeremic #
64 //# PROGRAMMER(S): Boris Jeremic #
65 //# #
66 //# #
67 //# DATE: May 28. - July 20 '93 #
68 //# UPDATE HISTORY: july 8. '93. BJtensor02 - BJtensor multiplication #
69 //# inner and outer products #
70 //# December 23 1993 print from the base class, operator==,#
71 //# macheps . . . #
72 //# August 22-29 '94 choped to separate files and worked on#
73 //# const and & issues #
74 //# August 30-31 '94 added use_def_dim to full the CC #
75 //# resolved problem with temoraries for #
76 //# operators + and - ( +=, -= ) #
77 //# January 16 '95 fixed the memory leakage introduced #
78 //# by previous work on +=, -+. I was #
79 //# by mistake decreasing #
80 //# this->pc_nDarray_rep->total_numb--; #
81 //# inststead of #
82 //# this->pc_nDarray_rep->n--; #
83 //# 28June2004 added val4 for efficiency still #
84 //# to be worked on #
85 //# #
86 //# #
87 //# #
88 //# #
89 //#############################################################################
90 //*/
91 
92 #ifndef NDARRAY_HH
93 #define NDARRAY_HH
94 
95 #include "utility/matrix/nDarray/nDarray_rep.h"
96 #include <string>
97 #include <iostream>
98 #include <boost/python.hpp>
99 #include "tmpl_operators.h"
100 
101 // forward reference
102 namespace XC {
103 
106 class nDarray
107  {
108  protected:
109  nDarray_rep pc_nDarray_rep;
110 
111  const double *data(void) const;
112  const std::vector<double> &vector_data(void) const;
113  void set_dim(const std::vector<int> &);
114  void rank(int);
115  private:
116 // int rank(void) const;
117  size_t total_number(void) const;
118  void total_number(size_t );
119  void clear_dim(void);
120  void clear_data(void);
121  void clear_dim_data(void);
122  const int &get_dim_pointer(void) const;
123  // int dim(int which) const;
124 
125  public:
126  nDarray(int rank_of_nDarray=1, const double &initval=0.0);// default constructor
127  nDarray(const std::vector<int> &pdim, const double *values);
128  nDarray(const std::vector<int> &pdim, const std::vector<double> &);
129  nDarray(const std::vector<int> &pdim, const boost::python::list &);
130  nDarray(const boost::python::list &, const boost::python::list &);
131  nDarray(const std::vector<int> &pdim, double initvalue);
132 
133  // special case for BJmatrix and BJvector . . .
134  nDarray(int rows, int cols, double *values);
135  nDarray(int rows, int cols, const std::vector<double> &values);
136  nDarray(int rows, int cols, const boost::python::list &);
137  nDarray(int rows, int cols, double initvalue);
138 
139 // special case when I don't want any initialization at all##
140  explicit nDarray(const std::string &){}
141 
142  nDarray(const std::string &flag, const std::vector<int> &pdim); // create a unit nDarray
143  inline virtual ~nDarray(void){};
144 
145 //##############################################################################
146 // copy only data because everything else has already been defined
147 // WATCH OUT IT HAS TO BE DEFINED BEFORE THIS FUNCTIONS IS CALLED
148 // use "from" and initialize already allocated nDarray from "from" values
149  void Initialize(const nDarray &from ); // initialize data only
150  void Initialize_all(const nDarray &from);// initialize and allocate all
151  // ( dimensions, rank and data )
152  // for BJtensor
153 
154  void Reset_to(const double &value); // reset data to "value"
155 
156  const std::vector<int> &dim(void) const;
157  boost::python::list dimPy(void) const;
158 
159  inline const double &operator()(int first) const
160  { return pc_nDarray_rep(first); }
161 
162  inline double &operator()(int first)
163  {
164  nDarray *this_no_const= const_cast<nDarray *>(this);
165  return this_no_const->operator()(first);
166  }
167 
168  inline const double &operator()(int first, int second) const
169  { return pc_nDarray_rep(first, second); }
170 
171  inline double &operator()(int first, int second)
172  {
173  nDarray *this_no_const= const_cast<nDarray *>(this);
174  return this_no_const->operator()(first, second);
175  }
176 
177  inline const double &operator()(int first, int second, int third) const
178  { return pc_nDarray_rep(first, second, third); }
179 
180  inline double &operator()(int first, int second, int third)
181  {
182  nDarray *this_no_const= const_cast<nDarray *>(this);
183  return this_no_const->operator()(first, second, third);
184  }
185  inline const double &operator()(int first, int second, int third, int fourth) const
186  { return pc_nDarray_rep(first, second, third, fourth); }
187 
188  inline double &operator()(int first, int second, int third, int fourth)
189  {
190  return pc_nDarray_rep(first, second, third, fourth);
191  }
192 
193  //const double &val(int subscript, ...) const;
194  //double &val(int subscript, ...);
195  inline const double &_val(const std::vector<int> &subscripts) const
196  { return _cval(subscripts); }
197  double &_val(const std::vector<int> &);
198  template<class...nums>
199  const double &val(nums...args) const
200  {
201  const std::vector<int> vec = {args...};
202  return _val(vec);
203  }
204  template<class...nums>
205  double &val(nums...args)
206  {
207  const std::vector<int> vec = {args...};
208  return _val(vec);
209  }
210 
211  const double &val4(int first, int second, int third, int fourth) const; // overloaded for FOUR arguments for operator * for two tensors
212  double &val4(int first, int second, int third, int fourth); // overloaded for FOUR arguments for operator * for two tensors
213 
214  //const double &cval(int subscript, ...) const;
215  const double &_cval(const std::vector<int> &) const;
216  template<class...nums>
217  const double &cval(nums...args) const
218  {
219  std::vector<int> vec = {args...};
220  return _cval(vec);
221  }
222 
223 //..
224 
225 
226 
227 //++ nDarray operator+( nDarray & rval); // nDarray addition
228 //....// This is from JOOP May/June 1990 after ARKoenig
229  nDarray& operator+=(const nDarray &); // nDarray addition
230 
231 
232 //++ nDarray operator-( nDarray & rval); // nDarray subtraction
233 //....// This is from JOOP May/June 1990 after ARKoenig
234  nDarray &operator-=(const nDarray & ); // nDarray subtraction
235 
236  nDarray operator+(const double &rval); // scalar addition
237  nDarray operator-(const double &rval); // scalar subtraction
238  nDarray &operator*=(const double &rval); // scalar multiplication
239  nDarray operator*(const double &rval) const; // scalar multiplication
240 
241  nDarray operator-(); // Unary minus
242 
243  double sum(void) const; // summ of all the elements
244  double trace(void) const; // trace of a 2-nd BJtensor, BJmatrix
245 
246  bool operator==(const nDarray &rval) const;
247 
248 // prebacen u nDarray 14 oktobra 1996
249  public:
250  nDarray eigenvalues(void);
251  nDarray eigenvectors(void);
252 
253 
254  double Frobenius_norm( void ); // return the Frobenius norm of
255  // BJmatrix, BJtensor, BJvector
256  double General_norm( double p ); // return the General p-th norm of
257  // BJmatrix, BJtensor, BJvector
258  public:
259  int rank(void) const;
260  int dim(int which) const;
261 
262  void output(std::ostream &os) const;
263  void outputshort(std::ostream &os) const;
264  void print(const std::string &name = "t",const std::string &msg = "Hi there#", std::ostream &os= std::cout) const;
265  void printshort(std::ostream &os, const std::string &msg = "Hi there#") const;
266  void mathprint(std::ostream &os) const;
267  friend std::string to_string(const nDarray &);
268  inline std::string toString(void) const
269  { return to_string(*this); }
270 
271 // from Numerical recipes in C
272  private:
273  static void tqli(std::vector<double> &d, std::vector<double> &, int n, std::vector<std::vector<double> > &z);
274  static void tred2(std::vector<std::vector<double> > &a, int n, std::vector<double> &d, std::vector<double> &e);
275  static void eigsrt(std::vector<double> &d, std::vector<std::vector<double> > &v, int n);
276 
277  };
278 
279 template nDarray operator*(const double & , const nDarray & );
280 template nDarray operator+(const nDarray & , const nDarray & );
281 template nDarray operator-(const nDarray & , const nDarray & );
282 std::ostream& operator<<(std::ostream &, const nDarray &);
283 std::string to_string(const nDarray &);
284 
285 } // end of XC namespace
286 
287 
288 #endif
289 
nDarray operator-()
unary minus
Definition: nDarray.cpp:665
void printshort(std::ostream &os, const std::string &msg="Hi there#") const
nDarray print function
Definition: nDarray.cpp:922
nDarray & operator+=(const nDarray &)
nDarray addition
Definition: nDarray.cpp:568
void Reset_to(const double &value)
Reset data to "value".
Definition: nDarray.cpp:381
bool operator==(const nDarray &rval) const
nDarray comparison returns true if they are equal.
Definition: nDarray.cpp:753
nDarray & operator*=(const double &rval)
scalar multiplication
Definition: nDarray.cpp:606
void outputshort(std::ostream &os) const
nDarray print function
Definition: nDarray.cpp:856
void print(const std::string &name="t", const std::string &msg="Hi there#", std::ostream &os=std::cout) const
nDarray print function
Definition: nDarray.cpp:832
Definition: bimap.h:33
void mathprint(std::ostream &os) const
nDarray print function for mathematica
Definition: nDarray.cpp:930
nDarray(int rank_of_nDarray=1, const double &initval=0.0)
Constructor.
Definition: nDarray.cpp:118
n-dimensional array.
Definition: nDarray.h:106
nDarray operator*(const double &rval) const
scalar multiplication
Definition: nDarray.cpp:614
Storage of n-dimensional array data.
Definition: nDarray_rep.h:112
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
nDarray operator+(const double &rval)
scalar addition
Definition: nDarray.cpp:597
boost::python::list dimPy(void) const
Return the array dimensions in a Python list.
Definition: nDarray.cpp:405
void output(std::ostream &os) const
Write this object to the argument stream.
Definition: nDarray.cpp:759