void promote_array(size_t n, const BaseArray<T>& s, BaseArray<T>& d) { vector<size_t> ex = s.getDims(); for (int i=0; i<n; i++) ex.push_back(1); d.setDims(ex); d.assign(s.getData()); }
void cast_array(const BaseArray<S>& a, BaseArray<T>& b) { b.setDims(a.getDims()); int numElems = a.getNumElems(); const S* src_data = a.getData(); T* dst_data = b.getData(); for (int i = 0; i < numElems; i++) *dst_data++ = (T)(*src_data++); }
void usub_array(const BaseArray<T>& a, BaseArray<T>& b) { b.setDims(a.getDims()); size_t numEle = a.getNumElems(); for (size_t i = 1; i <= numEle; i++) { b(i) = -a(i); } }
void divide_array(const BaseArray<T>& inputArray, const T &b, BaseArray<T>& outputArray) { size_t nelems = inputArray.getNumElems(); if (outputArray.getNumElems() != nelems) { outputArray.setDims(inputArray.getDims()); } const T* data = inputArray.getData(); T* aim = outputArray.getData(); std::transform(data, data + nelems, aim, std::bind2nd(std::divides<T>(), b)); }
void convertArrayLayout(const BaseArray<S> &s, BaseArray<T> &d) { size_t ndims = s.getNumDims(); if (ndims != d.getNumDims()) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong dimensions in convertArrayLayout"); vector<size_t> sdims = s.getDims(); vector<size_t> ddims(ndims); for (size_t dim = 1; dim <= ndims; dim++) ddims[ndims - dim] = sdims[dim - 1]; d.resize(ddims); convertArrayDim(1, s, sdims, d, ddims); }
void subtract_array_scalar(const BaseArray<T>& inputArray, T b, BaseArray<T>& outputArray) { size_t dim = inputArray.getNumElems(); if(dim > 0) { outputArray.setDims(inputArray.getDims()); const T* data = inputArray.getData(); T* aim = outputArray.getData(); std::transform (data, data + inputArray.getNumElems(), aim, std::bind2nd(std::minus<T>(), b)); } }
void pow_array_scalar(const BaseArray<double> &inputArray, T exponent, BaseArray<double> &outputArray) { size_t nelems = inputArray.getNumElems(); if (outputArray.getNumElems() != nelems) outputArray.setDims(inputArray.getDims()); const double *data = inputArray.getData(); double *dest = outputArray.getData(); double *end = dest + nelems; while (dest != end) *dest++ = pow(*data++, exponent); }
void multiply_array(const BaseArray<T>& inputArray, const T &b, BaseArray<T>& outputArray) { size_t dim = inputArray.getNumElems(); if(dim > 0) { outputArray.setDims(inputArray.getDims()); const T* data = inputArray.getData(); T* aim = outputArray.getData(); std::transform (data, data + inputArray.getNumElems(), aim, std::bind2nd(std::multiplies<T>(), b)); } };
void add_array(const BaseArray<T>& leftArray, const BaseArray<T>& rightArray, BaseArray<T>& resultArray) { size_t dimLeft = leftArray.getNumElems(); size_t dimRight = rightArray.getNumElems(); if(dimLeft != dimRight) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Right and left array must have the same size for element wise addition"); resultArray.setDims(leftArray.getDims()); const T* data1 = leftArray.getData(); const T* data2 = rightArray.getData(); T* aim = resultArray.getData(); std::transform(data1, data1 + leftArray.getNumElems(), data2, aim, std::plus<T>()); }
void multiply_array_elem_wise(const BaseArray<T> &leftArray, const BaseArray<T> &rightArray, BaseArray<T> &resultArray) { size_t dimLeft = leftArray.getNumElems(); size_t dimRight = rightArray.getNumElems(); if(dimLeft != dimRight) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Right and left array must have the same size for element wise multiplication"); resultArray.setDims(leftArray.getDims()); const T* leftData = leftArray.getData(); const T* rightData = rightArray.getData(); T* aim = resultArray.getData(); std::transform (leftData, leftData + leftArray.getNumElems(), rightData, aim, std::multiplies<T>()); }
void transpose_array(const BaseArray<T>& x, BaseArray<T>& a) { size_t ndims = x.getNumDims(); if(ndims < 2 || ndims != a.getNumDims()) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong dimensions in transpose_array"); vector<size_t> ex = x.getDims(); std::swap(ex[0], ex[1]); a.setDims(ex); vector<Slice> sx(ndims); vector<Slice> sa(ndims); for (int i = 1; i <= x.getDim(1); i++) { sa[1] = sx[0] = Slice(i); ArraySlice<T>(a, sa).assign(ArraySliceConst<T>(x, sx)); } }
void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArray, BaseArray<T> &resultArray) { size_t leftNumDims = leftArray.getNumDims(); size_t rightNumDims = rightArray.getNumDims(); size_t matchDim = rightArray.getDim(1); resultArray.setDims(leftArray.getDims()); if (leftArray.getDim(leftNumDims) != matchDim) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong sizes in multiply_array"); if (leftNumDims == 1 && rightNumDims == 2) { size_t rightDim = rightArray.getDim(2); for (size_t j = 1; j <= rightDim; j++) { T val = T(); for (size_t k = 1; k <= matchDim; k++) val += leftArray(k) * rightArray(k, j); resultArray(j) = val; } } else if (leftNumDims == 2 && rightNumDims == 1) { size_t leftDim = leftArray.getDim(1); for (size_t i = 1; i <= leftDim; i++) { T val = T(); for (size_t k = 1; k <= matchDim; k++) val += leftArray(i, k) * rightArray(k); resultArray(i) = val; } } else if (leftNumDims == 2 && rightNumDims == 2) { size_t leftDim = leftArray.getDim(1); size_t rightDim = rightArray.getDim(2); for (size_t i = 1; i <= leftDim; i++) { for (size_t j = 1; j <= rightDim; j++) { T val = T(); for (size_t k = 1; k <= matchDim; k++) val += leftArray(i, k) * rightArray(k, j); resultArray(i, j) = val; } } } else throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Unsupported dimensions in multiply_array"); }