Skip to content

Commit

Permalink
implement POW_ARRAY_SCALAR in Cpp runtime
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25720 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
rfranke committed Apr 23, 2015
1 parent 7bf8847 commit 681f96c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -11963,12 +11963,19 @@ template daeExpBinary(Operator it, Exp exp1, Exp exp2, Context context, Text &pr
case ADD_ARRAY_SCALAR(__) then "daeExpBinary:ERR ADD_ARRAY_SCALAR not supported"
case SUB_SCALAR_ARRAY(__) then "daeExpBinary:ERR SUB_SCALAR_ARRAY not supported"
case MUL_SCALAR_PRODUCT(__) then
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int>'
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int'
case T_ARRAY(ty=T_ENUMERATION(__)) then 'int'
else 'double'
'dot_array<<%type%>>(<%e1%>, <%e2%>)'
case DIV_SCALAR_ARRAY(__) then "daeExpBinary:ERR DIV_SCALAR_ARRAY not supported"
case POW_ARRAY_SCALAR(__) then "daeExpBinary:ERR POW_ARRAY_SCALAR not supported"
case POW_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then
let dimstr = checkDimension(dims)
let type = "double"
let var = match dimstr // copy to contiguous memory and pow in situ
case "" then tempDecl1('DynArrayDim<%listLength(dims)%><<%type%>>', e1, &preExp)
else tempDecl1('StatArrayDim<%listLength(dims)%><<%type%>, <%dimstr%>>', e1, &preExp)
let &preExp += 'pow_array_scalar(<%var%>, <%e2%>, <%var%>);<%\n%>'
'<%var%>'
case POW_SCALAR_ARRAY(__) then "daeExpBinary:ERR POW_SCALAR_ARRAY not supported"
case POW_ARR(__) then "daeExpBinary:ERR POW_ARR not supported"
case POW_ARR2(__) then "daeExpBinary:ERR POW_ARR2 not supported"
Expand Down
17 changes: 17 additions & 0 deletions SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -257,6 +257,20 @@ void fill_array(BaseArray<T>& inputArray, T b)
std::fill(data, data + nelems, b);
};

template <typename T>
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);
}

template < typename T >
void subtract_array(BaseArray<T>& leftArray, BaseArray<T>& rightArray, BaseArray<T>& resultArray)
{
Expand Down Expand Up @@ -471,6 +485,9 @@ template void BOOST_EXTENSION_EXPORT_DECL fill_array(BaseArray<double>& inputArr
template void BOOST_EXTENSION_EXPORT_DECL fill_array(BaseArray<int>& inputArray, int b);
template void BOOST_EXTENSION_EXPORT_DECL fill_array(BaseArray<bool>& inputArray, bool b);

template void BOOST_EXTENSION_EXPORT_DECL pow_array_scalar(const BaseArray<double> &inputArray, double exponent, BaseArray<double> &outputArray);
template void BOOST_EXTENSION_EXPORT_DECL pow_array_scalar(const BaseArray<double> &inputArray, int exponent, BaseArray<double> &outputArray);

template void BOOST_EXTENSION_EXPORT_DECL subtract_array(BaseArray<double>& leftArray, BaseArray<double>& rightArray, BaseArray<double>& resultArray);
template void BOOST_EXTENSION_EXPORT_DECL subtract_array(BaseArray<int>& leftArray, BaseArray<int>& rightArray, BaseArray<int>& resultArray);
template void BOOST_EXTENSION_EXPORT_DECL subtract_array(BaseArray<bool>& leftArray, BaseArray<bool>& rightArray, BaseArray<bool>& resultArray);
Expand Down
6 changes: 6 additions & 0 deletions SimulationRuntime/cpp/Include/Core/Math/ArrayOperations.h
Expand Up @@ -58,6 +58,12 @@ void divide_array( BaseArray<T> & inputArray ,const T &b, BaseArray<T> & outputA
template < typename T >
void fill_array( BaseArray<T> & inputArray , T b);

/**
* Element wise exponentiation
*/
template <typename T>
void pow_array_scalar(const BaseArray<double> &inputArray, T exponent, BaseArray<double> &outputArray);

template < typename T >
void subtract_array( BaseArray<T> & leftArray , BaseArray<T> & rightArray, BaseArray<T> & resultArray );

Expand Down

0 comments on commit 681f96c

Please sign in to comment.