/[escript]/trunk/escript/src/Data.cpp
ViewVC logotype

Diff of /trunk/escript/src/Data.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/escript/src/Data.cpp revision 1312 by ksteube, Mon Sep 24 06:18:44 2007 UTC temp/escript/src/Data.cpp revision 1387 by trankine, Fri Jan 11 07:45:26 2008 UTC
# Line 19  Line 19 
19  #include "DataConstant.h"  #include "DataConstant.h"
20  #include "DataTagged.h"  #include "DataTagged.h"
21  #include "DataEmpty.h"  #include "DataEmpty.h"
 #include "DataArray.h"  
22  #include "DataArrayView.h"  #include "DataArrayView.h"
23  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
24  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
# Line 61  Data::Data(double value, Line 60  Data::Data(double value,
60    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
61      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
62    }    }
63    DataArray temp(dataPointShape,value);  
64    initialise(temp.getView(),what,expanded);    int len = DataArrayView::noValues(dataPointShape);
65      DataVector temp_data(len,value,len);
66      DataArrayView temp_dataView(temp_data, dataPointShape);
67    
68      initialise(temp_dataView, what, expanded);
69    
70    m_protected=false;    m_protected=false;
71  }  }
72    
# Line 71  Data::Data(double value, Line 75  Data::Data(double value,
75         const FunctionSpace& what,         const FunctionSpace& what,
76             bool expanded)             bool expanded)
77  {  {
78    DataArray temp(dataPointShape,value);    int len = DataArrayView::noValues(dataPointShape);
79    initialise(temp.getView(),what,expanded);  
80      DataVector temp_data(len,value,len);
81      DataArrayView temp_dataView(temp_data, dataPointShape);
82    
83      initialise(temp_dataView, what, expanded);
84    
85    m_protected=false;    m_protected=false;
86  }  }
87    
# Line 155  Data::Data(const object& value, Line 164  Data::Data(const object& value,
164    m_protected=false;    m_protected=false;
165  }  }
166    
167    
168  Data::Data(const object& value,  Data::Data(const object& value,
169             const Data& other)             const Data& other)
170  {  {
171    
172      numeric::array asNumArray(value);
173    
174    
175      // extract the shape of the numarray
176      DataArrayView::ShapeType tempShape;
177      for (int i=0; i < asNumArray.getrank(); i++) {
178        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
179      }
180      // get the space for the data vector
181      int len = DataArrayView::noValues(tempShape);
182      DataVector temp_data(len, 0.0, len);
183      DataArrayView temp_dataView(temp_data, tempShape);
184      temp_dataView.copy(asNumArray);
185    
186    //    //
187    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
188    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
189    // will assume the point data shape of other.    // will assume the point data shape of other.
190    DataArray temp(value);  
191    if (temp.getView().getRank()==0) {    if (temp_dataView.getRank()==0) {
192      //      int len = DataArrayView::noValues(other.getPointDataView().getShape());
193      // Create a DataArray with the scalar value for all elements  
194      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      DataVector temp2_data(len, temp_dataView(), len);
195      initialise(temp2.getView(),other.getFunctionSpace(),false);      DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
196        initialise(temp2_dataView, other.getFunctionSpace(), false);
197    
198    } else {    } else {
199      //      //
200      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
201      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp_dataView, other.getFunctionSpace(), false);
202    }    }
203    m_protected=false;    m_protected=false;
204  }  }
# Line 400  Data::tag() Line 427  Data::tag()
427  Data  Data
428  Data::oneOver() const  Data::oneOver() const
429  {  {
430    return escript::unaryOp(*this,bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
431  }  }
432    
433  Data  Data
434  Data::wherePositive() const  Data::wherePositive() const
435  {  {
436    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
437  }  }
438    
439  Data  Data
440  Data::whereNegative() const  Data::whereNegative() const
441  {  {
442    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
443  }  }
444    
445  Data  Data
446  Data::whereNonNegative() const  Data::whereNonNegative() const
447  {  {
448    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
449  }  }
450    
451  Data  Data
452  Data::whereNonPositive() const  Data::whereNonPositive() const
453  {  {
454    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
455  }  }
456    
457  Data  Data
458  Data::whereZero(double tol) const  Data::whereZero(double tol) const
459  {  {
460    Data dataAbs=abs();    Data dataAbs=abs();
461    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
462  }  }
463    
464  Data  Data
465  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
466  {  {
467    Data dataAbs=abs();    Data dataAbs=abs();
468    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
469  }  }
470    
471  Data  Data
# Line 877  Data::integrate() const Line 904  Data::integrate() const
904  Data  Data
905  Data::sin() const  Data::sin() const
906  {  {
907    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
908  }  }
909    
910  Data  Data
911  Data::cos() const  Data::cos() const
912  {  {
913    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
914  }  }
915    
916  Data  Data
917  Data::tan() const  Data::tan() const
918  {  {
919    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
920  }  }
921    
922  Data  Data
923  Data::asin() const  Data::asin() const
924  {  {
925    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
926  }  }
927    
928  Data  Data
929  Data::acos() const  Data::acos() const
930  {  {
931    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
932  }  }
933    
934    
935  Data  Data
936  Data::atan() const  Data::atan() const
937  {  {
938    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
939  }  }
940    
941  Data  Data
942  Data::sinh() const  Data::sinh() const
943  {  {
944    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
945    
946  }  }
947    
948  Data  Data
949  Data::cosh() const  Data::cosh() const
950  {  {
951    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
952  }  }
953    
954  Data  Data
955  Data::tanh() const  Data::tanh() const
956  {  {
957    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
958  }  }
959    
960    
# Line 936  Data::erf() const Line 964  Data::erf() const
964  #ifdef _WIN32  #ifdef _WIN32
965    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
966  #else  #else
967    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);    return C_TensorUnaryOperation(*this, ::erf);
968  #endif  #endif
969  }  }
970    
# Line 944  Data Line 972  Data
972  Data::asinh() const  Data::asinh() const
973  {  {
974  #ifdef _WIN32  #ifdef _WIN32
975    return escript::unaryOp(*this,escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
976  #else  #else
977    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return C_TensorUnaryOperation(*this, ::asinh);
978  #endif  #endif
979  }  }
980    
# Line 954  Data Line 982  Data
982  Data::acosh() const  Data::acosh() const
983  {  {
984  #ifdef _WIN32  #ifdef _WIN32
985    return escript::unaryOp(*this,escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
986  #else  #else
987    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return C_TensorUnaryOperation(*this, ::acosh);
988  #endif  #endif
989  }  }
990    
# Line 964  Data Line 992  Data
992  Data::atanh() const  Data::atanh() const
993  {  {
994  #ifdef _WIN32  #ifdef _WIN32
995    return escript::unaryOp(*this,escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
996  #else  #else
997    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return C_TensorUnaryOperation(*this, ::atanh);
998  #endif  #endif
999  }  }
1000    
1001  Data  Data
1002  Data::log10() const  Data::log10() const
1003  {  {
1004    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1005  }  }
1006    
1007  Data  Data
1008  Data::log() const  Data::log() const
1009  {  {
1010    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1011  }  }
1012    
1013  Data  Data
1014  Data::sign() const  Data::sign() const
1015  {  {
1016    return escript::unaryOp(*this,escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1017  }  }
1018    
1019  Data  Data
1020  Data::abs() const  Data::abs() const
1021  {  {
1022    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1023  }  }
1024    
1025  Data  Data
1026  Data::neg() const  Data::neg() const
1027  {  {
1028    return escript::unaryOp(*this,negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1029  }  }
1030    
1031  Data  Data
# Line 1012  Data::pos() const Line 1040  Data::pos() const
1040  Data  Data
1041  Data::exp() const  Data::exp() const
1042  {  {
1043    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1044  }  }
1045    
1046  Data  Data
1047  Data::sqrt() const  Data::sqrt() const
1048  {  {
1049    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1050  }  }
1051    
1052  double  double
# Line 1486  Data::powO(const boost::python::object& Line 1514  Data::powO(const boost::python::object&
1514  Data  Data
1515  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1516  {  {
1517    Data result;    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   if (getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result.binaryOp(*this,escript::rpow);  
   } else {  
      result.copy(*this);  
      result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   }  
   return result;  
1518  }  }
1519    
   
1520  //  //
1521  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1522  Data  Data
1523  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1524  {  {
1525    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result+=left;  
   } else {  
      result.copy(left);  
      result+=right;  
   }  
   return result;  
1526  }  }
1527    
1528  //  //
# Line 1521  escript::operator+(const Data& left, con Line 1530  escript::operator+(const Data& left, con
1530  Data  Data
1531  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1532  {  {
1533    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.neg();  
      result+=left;  
   } else {  
      result.copy(left);  
      result-=right;  
   }  
   return result;  
1534  }  }
1535    
1536  //  //
# Line 1539  escript::operator-(const Data& left, con Line 1538  escript::operator-(const Data& left, con
1538  Data  Data
1539  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1540  {  {
1541    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result*=left;  
   } else {  
      result.copy(left);  
      result*=right;  
   }  
   return result;  
1542  }  }
1543    
1544  //  //
# Line 1557  escript::operator*(const Data& left, con Line 1546  escript::operator*(const Data& left, con
1546  Data  Data
1547  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1548  {  {
1549    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.oneOver();  
      result*=left;  
   } else {  
      result.copy(left);  
      result/=right;  
   }  
   return result;  
1550  }  }
1551    
1552  //  //
# Line 1789  Data::setTaggedValue(int tagKey, Line 1768  Data::setTaggedValue(int tagKey,
1768    }    }
1769    //    //
1770    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1771    tag();    if (isConstant()) tag();
1772    
1773    if (!isTagged()) {    numeric::array asNumArray(value);
1774      throw DataException("Error - DataTagged conversion failed!!");  
1775    
1776      // extract the shape of the numarray
1777      DataArrayView::ShapeType tempShape;
1778      for (int i=0; i < asNumArray.getrank(); i++) {
1779        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1780    }    }
1781    
1782    //    // get the space for the data vector
1783    // Construct DataArray from boost::python::object input value    int len = DataArrayView::noValues(tempShape);
1784    DataArray valueDataArray(value);    DataVector temp_data(len, 0.0, len);
1785      DataArrayView temp_dataView(temp_data, tempShape);
1786      temp_dataView.copy(asNumArray);
1787    
1788    //    //
1789    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1790    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,temp_dataView);
1791  }  }
1792    
1793  void  void
# Line 1813  Data::setTaggedValueFromCPP(int tagKey, Line 1799  Data::setTaggedValueFromCPP(int tagKey,
1799    }    }
1800    //    //
1801    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1802    tag();    if (isConstant()) tag();
   
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
1803    
1804    //    //
1805    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
# Line 1827  Data::setTaggedValueFromCPP(int tagKey, Line 1809  Data::setTaggedValueFromCPP(int tagKey,
1809  int  int
1810  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1811  {  {
1812    return m_data->getTagNumber(dpno);    return getFunctionSpace().getTagFromSampleNo(dpno);
1813  }  }
1814    
1815  void  void

Legend:
Removed from v.1312  
changed lines
  Added in v.1387

  ViewVC Help
Powered by ViewVC 1.1.26