Skip to content

Commit 511bafb

Browse files
committed
fix and generalize transpose_array
also unify ordering of call arguments for transpose_array and cat_array (return comes last) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25593 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 78e0e43 commit 511bafb

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Compiler/Template/CodegenCpp.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11556,7 +11556,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
1155611556
'vector<BaseArray<<%ty_str%>>* > <%tvar%>_list;
1155711557
<%tvar%>_list.push_back(&<%a0str%>);
1155811558
<%arrays_exp%>
11559-
cat_array<<%ty_str%> >(<%dim_exp%>,<%tvar%>, <%tvar%>_list );
11559+
cat_array<<%ty_str%>>(<%dim_exp%>, <%tvar%>_list, <%tvar%>);
1156011560
'
1156111561
'<%tvar%>'
1156211562

@@ -11584,7 +11584,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
1158411584
let type_str = expTypeFromExpShort(A)
1158511585
let arr_tp_str = '<%expTypeFromExpArray(A)%>'
1158611586
let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/)
11587-
let &preExp += 'transpose_array< <%type_str%> >(<%tvar%>,<%var1%>);<%\n%>'
11587+
let &preExp += 'transpose_array<<%type_str%>>(<%var1%>, <%tvar%>);<%\n%>'
1158811588
'<%tvar%>'
1158911589

1159011590
case CALL(path=IDENT(name="cross"), expLst={v1, v2},attr=CALL_ATTR(ty=ty as T_ARRAY(dims=dims))) then

SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Core/Modelica.h>
22
#include <Core/Math/ArrayOperations.h>
3+
#include <Core/Math/ArraySlice.h>
34
#include <sstream>
45
#include <stdio.h>
56
using namespace std;
@@ -19,7 +20,7 @@ size_t getNextIndex(vector<size_t> idx,size_t k)
1920
Concatenates n real arrays along the k:th dimension.
2021
*/
2122
template < typename T >
22-
void cat_array (int k,BaseArray<T>& a, vector<BaseArray<T>* >& x )
23+
void cat_array(int k, vector<BaseArray<T>* >& x, BaseArray<T>& a)
2324
{
2425
unsigned int new_k_dim_size = 0;
2526
unsigned int n = x.size();
@@ -159,19 +160,26 @@ void promote_array(unsigned int n,BaseArray<T>& s,BaseArray<T>& d)
159160
d.assign( data );
160161
}
161162

162-
163+
/**
164+
* permutes the first two dimensions of x into a
165+
*/
163166
template < typename T >
164-
void transpose_array (BaseArray< T >& a, BaseArray< T >& x )
167+
void transpose_array(BaseArray< T >& x, BaseArray< T >& a)
165168

166169
{
167-
if(a.getNumDims()!=2 || x.getNumDims()!=2)
168-
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Erro in transpose_array, number of dimensions does not match");
169-
170-
vector<size_t> ex = x.getDims();
171-
std::swap( ex[0], ex[1] );
172-
a.setDims(ex);
173-
a.assign(x);
174-
170+
size_t ndims = x.getNumDims();
171+
if(ndims < 2 || ndims != a.getNumDims())
172+
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
173+
"Wrong dimensions in transpose_array");
174+
vector<size_t> ex = x.getDims();
175+
std::swap(ex[0], ex[1]);
176+
a.setDims(ex);
177+
vector<Slice> sx(ndims);
178+
vector<Slice> sa(ndims);
179+
for (int i = 1; i <= x.getDim(1); i++) {
180+
sa[1] = sx[0] = Slice(i);
181+
ArraySlice<T>(a, sa).assign(ArraySlice<T>(x, sx));
182+
}
175183
}
176184

177185
template < typename T>
@@ -390,13 +398,13 @@ void convertExternalF77(const BaseArray<S> &s, BaseArray<T> &d) {
390398
/*
391399
Explicit template instantiation for double, int, bool
392400
*/
393-
template void BOOST_EXTENSION_EXPORT_DECL cat_array<double>(int k,BaseArray<double>& a, vector<BaseArray<double>* >& x);
394-
template void BOOST_EXTENSION_EXPORT_DECL cat_array<int>(int k,BaseArray<int>& a, vector<BaseArray<int>* >& x);
395-
template void BOOST_EXTENSION_EXPORT_DECL cat_array<bool>(int k,BaseArray<bool>& a, vector<BaseArray<bool>* >& x);
401+
template void BOOST_EXTENSION_EXPORT_DECL cat_array<double>(int k, vector<BaseArray<double>* >& x, BaseArray<double>& a);
402+
template void BOOST_EXTENSION_EXPORT_DECL cat_array<int>(int k, vector<BaseArray<int>* >& x, BaseArray<int>& a);
403+
template void BOOST_EXTENSION_EXPORT_DECL cat_array<bool>(int k, vector<BaseArray<bool>* >& x, BaseArray<bool>& a);
396404

397-
template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<double>& a, BaseArray< double >& x);
398-
template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<int>& a, BaseArray< int >& x);
399-
template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<bool>& a, BaseArray< bool >& x);
405+
template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<double>& x, BaseArray< double >& a);
406+
template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<int>& x, BaseArray< int >& a);
407+
//template void BOOST_EXTENSION_EXPORT_DECL transpose_array(BaseArray<bool>& x, BaseArray< bool >& a);
400408

401409
template void BOOST_EXTENSION_EXPORT_DECL promote_array(unsigned int n, BaseArray<double>& s, BaseArray<double>& d);
402410
template void BOOST_EXTENSION_EXPORT_DECL promote_array(unsigned int n, BaseArray<int>& s, BaseArray<int>& d);

SimulationRuntime/cpp/Include/Core/Math/ArrayOperations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ size_t getNextIndex(vector<size_t> idx,size_t k);
2727
Concatenates n real arrays along the k:th dimension.
2828
*/
2929
template < typename T >
30-
void cat_array (int k,BaseArray<T>& a, vector<BaseArray<T>* >& x );
30+
void cat_array(int k, vector<BaseArray<T>* >& x, BaseArray<T>& a);
3131

3232

3333
template < typename T >
34-
void transpose_array (BaseArray< T >& a, BaseArray< T >& x );
34+
void transpose_array(BaseArray< T >& x, BaseArray< T >& a);
3535

3636
/*
3737
creates an array (d) for passed multi array shape (sp) and initialized it with elements from passed source array (s)

0 commit comments

Comments
 (0)