Changeset 25479


Ignore:
Timestamp:
2015-04-09T21:30:17+02:00 (9 years ago)
Author:
rfranke
Message:

implement multiply_array for 1D and 2D arrays

Nothing to be proud of ... but better than a missing matrix multiplication.

Location:
trunk/SimulationRuntime/cpp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp

    r24878 r25479  
    185185};
    186186
     187template <typename T>
     188void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArray, BaseArray<T> &resultArray)
     189{
     190  size_t leftNumDims = leftArray.getNumDims();
     191  size_t rightNumDims = rightArray.getNumDims();
     192  size_t matchDim = rightArray.getDim(1);
     193  if (leftArray.getDim(leftNumDims) != matchDim)
     194    throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
     195                                  "Wrong sizes in multiply_array");
     196  if (matchDim == 0) {
     197    fill_array(resultArray, T()); // zero
     198  }
     199  else if (leftNumDims == 1 && rightNumDims == 2) {
     200    size_t rightDim = rightArray.getDim(2);
     201    for (size_t j = 1; j <= rightDim; j++) {
     202      T val = leftArray(1) * rightArray(1, j);
     203      for (size_t k = 2; k <= matchDim; k++)
     204        val += leftArray(k) * rightArray(k, j);
     205      resultArray(j) = val;
     206    }
     207  }
     208  else if (leftNumDims == 2 && rightNumDims == 1) {
     209    size_t leftDim = leftArray.getDim(1);
     210    for (size_t i = 1; i <= leftDim; i++) {
     211      T val = leftArray(i, 1) * rightArray(1);
     212      for (size_t k = 2; k <= matchDim; k++)
     213        val += leftArray(i, k) * rightArray(k);
     214      resultArray(i) = val;
     215    }
     216  }
     217  else if (leftNumDims == 2 && rightNumDims == 2) {
     218    size_t leftDim = leftArray.getDim(1);
     219    size_t rightDim = rightArray.getDim(2);
     220    for (size_t i = 1; i <= leftDim; i++) {
     221      for (size_t j = 1; j <= rightDim; j++) {
     222        T val = leftArray(i, 1) * rightArray(1, j);
     223        for (size_t k = 2; k <= matchDim; k++)
     224          val += leftArray(i, k) * rightArray(k, j);
     225        resultArray(i, j) = val;
     226      }
     227    }
     228  }
     229  else
     230    throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
     231                                  "Unsupported dimensions in multiply_array");
     232}
     233
    187234template < typename T>
    188235void divide_array(BaseArray<T>& inputArray, const T &b, BaseArray<T>& outputArray)
     
    346393template void BOOST_EXTENSION_EXPORT_DECL multiply_array(BaseArray<bool>& inputArray, const bool &b, BaseArray<bool> & outputArray);
    347394
     395template void BOOST_EXTENSION_EXPORT_DECL multiply_array(const BaseArray<double> &leftArray, const BaseArray<double> &rightArray, BaseArray<double> &resultArray);
     396template void BOOST_EXTENSION_EXPORT_DECL multiply_array(const BaseArray<int> &leftArray, const BaseArray<int> &rightArray, BaseArray<int> &resultArray);
     397template void BOOST_EXTENSION_EXPORT_DECL multiply_array(const BaseArray<bool> &leftArray, const BaseArray<bool> &rightArray, BaseArray<bool> &resultArray);
     398
    348399template void BOOST_EXTENSION_EXPORT_DECL divide_array(BaseArray<double>& inputArray, const double &b, BaseArray<double>& outputArray);
    349400template void BOOST_EXTENSION_EXPORT_DECL divide_array(BaseArray<int>& inputArray, const int &b, BaseArray<int>& outputArray);
  • trunk/SimulationRuntime/cpp/Include/Core/Math/Array.h

    r25448 r25479  
    4040  };
    4141
    42   virtual T& operator()(size_t  i, size_t j)
     42  virtual T& operator()(size_t i, size_t j)
     43  {
     44    throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Wrong virtual Array operator call");
     45  };
     46
     47  virtual const T& operator()(size_t i, size_t j) const
    4348  {
    4449    throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Wrong virtual Array operator call");
     
    14651470    return _multi_array[i][j];
    14661471  }
     1472  inline virtual const T& operator()(size_t i, size_t j) const
     1473  {
     1474    return _multi_array[i][j];
     1475  }
    14671476
    14681477  void setDims(size_t size1, size_t size2)
  • trunk/SimulationRuntime/cpp/Include/Core/Math/ArrayOperations.h

    r22833 r25479  
    4444void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d);
    4545
    46 
    47 
    4846template < typename T >
    4947void promote_array(unsigned int n,BaseArray<T>& s,BaseArray<T>& d);
    50 
    51 
    5248
    5349template < typename T>
    5450void multiply_array( BaseArray<T> & inputArray ,const T &b, BaseArray<T> & outputArray  );
    5551
     52template <typename T>
     53void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArray, BaseArray<T> &resultArray);
    5654
    5755template < typename T>
    5856void divide_array( BaseArray<T> & inputArray ,const T &b, BaseArray<T> & outputArray  );
    59 
    6057
    6158template < typename T >
  • trunk/SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h

    r25453 r25479  
    252252  }
    253253
    254   virtual T& operator()(size_t  i, size_t j) {
     254  virtual T& operator()(size_t i, size_t j) {
     255    accessElement(2, i, j);
     256  }
     257
     258  virtual const T& operator()(size_t i, size_t j) const {
    255259    accessElement(2, i, j);
    256260  }
Note: See TracChangeset for help on using the changeset viewer.