Skip to content

Commit

Permalink
real_array and integer_array are now classes.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@725 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
ankar committed Feb 12, 2002
1 parent ca22911 commit 318d934
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 32 deletions.
77 changes: 75 additions & 2 deletions mosh/src/runtime/modelica_array.hpp
Expand Up @@ -24,23 +24,34 @@ class modelica_array

// // --------- Dimensionality conversion functions ---------
// /// Converts a to a scalar
// double scalar();
Tp scalar() const;

// /// Converts a to a vector.
// modelica_real_array vector();

// /// Converts a to a matrix.
// modelica_real_array matrix();

friend modelica_array<Tp> fill_array<Tp>(Tp s,std::vector<int> dims);

friend modelica_array<Tp> create_array<Tp>(std::vector<Tp> data);
friend modelica_array<Tp> create_array<Tp>(std::vector<modelica_array<Tp> > arrays);

void print_dims();
void print_data() const;
// void print();
modelica_array<Tp> slice(std::vector<int> idx);

protected:
modelica_array() {};
modelica_array(std::vector<int> dims) { m_dim_size = dims;};
modelica_array(std::vector<int> dims);
modelica_array(std::vector<int> dims,std::vector<Tp> scalars);

// /// Returns the number of elements in the matrix.
int nr_of_elements() const;



typedef vector<Tp>::iterator data_iterator;
vector<Tp> m_data;

Expand All @@ -50,6 +61,13 @@ class modelica_array

};

template <class Tp>
modelica_array<Tp>::modelica_array(std::vector<int> dims)
{
m_dim_size = dims;
m_data.resize(nr_of_elements());
}

template <class Tp>
modelica_array<Tp>::modelica_array(std::vector<int> dims,std::vector<Tp> scalars)
{
Expand Down Expand Up @@ -78,6 +96,47 @@ int modelica_array<Tp>::size(int dim) const
return m_dim_size[dim];
}

template <typename Tp>
Tp modelica_array<Tp>::scalar() const
{
for (int i = 0; i < m_dim_size.size(); ++i)
{
assert(m_dim_size[i] == 1);
}

}

template <typename Tp>
modelica_array<Tp> create_array(std::vector<Tp> data)
{
modelica_array<Tp> result(std::vector<int>(1,data.size()),data);
return result;
}

template <typename Tp>
modelica_array<Tp> create_array(std::vector<modelica_array<Tp> > arrays)
{
modelica_array<Tp> result;
std::vector<int> dims(1,1);
dims = arrays[1].m_dim_size;
dims.insert(dims.end(),result.m_dim_size.begin(),m_dim_size.end());

return result;

}

template <class Tp>
modelica_array<Tp> fill_array(Tp s,std::vector<int> dims)
{
modelica_array<Tp> result(dims);

for (int i = 0; i < result.nr_of_elements(); ++i)
{
result.m_data[i] = s;
}
return result;
}

template <class Tp>
void modelica_array<Tp>::print_dims()
{
Expand All @@ -97,6 +156,20 @@ modelica_array<Tp> modelica_array<Tp>::slice(std::vector<int> idx)
// Copy data
}

template <class Tp>
int modelica_array<Tp>::nr_of_elements() const
{
return std::accumulate(m_dim_size.begin(),m_dim_size.end(),1,std::multiplies<int>());
}

template <class Tp>
void modelica_array<Tp>::print_data() const
{
for (int i = 0; i < m_data.size();++i)
{
cout << "m_data[" << i <<"] = " << m_data[i] << endl;
}
}
// template <class Tp>
// void modelica_array<Tp>::print()
// {
Expand Down
126 changes: 96 additions & 30 deletions mosh/src/runtime/numerical_array.hpp
Expand Up @@ -23,15 +23,16 @@ class numerical_array : public modelica_array<Tp>
Tp sum() const;


// /// Returns an array filled with zeros with dimensions given by dim.
// friend modelica_real_array zeros(std::vector<Tp> dim);

/// Returns a square matrix with the elements of v on the diagonal and all other elements set to zero.
friend numerical_array<Tp> diagonal<Tp>(const numerical_array<Tp>& arr);
numerical_array() {};



numerical_array() {};
numerical_array(std::vector<int> dims) : modelica_array<Tp>(dims) {};
numerical_array(std::vector<int> dims,std::vector<Tp> scalars) : modelica_array<Tp>(dims,scalars) {};

numerical_array(modelica_array<Tp>& arr) : modelica_array<Tp>(arr) {};
protected:

};
Expand Down Expand Up @@ -65,51 +66,116 @@ numerical_array<Tp> diagonal(const numerical_array<Tp>& arr)
{
assert(arr.ndims() == 1);

numerical_array<Tp> result(std::vector<int>(2,arr.m_data.size()));
int n = arr.m_data.size();
numerical_array<Tp> result(std::vector<int>(2,n));

for (int i = 0; i < arr.ndims(); ++i)
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < arr.ndims(); ++j)
for (int j = 0; j < n; ++j)
{
result.m_data[i*arr.ndims() + j] = (i==j) ? arr.m_data[i] : 0;
result.m_data[i*n + j] = (i==j) ? arr.m_data[i] : 0;
}
}
return result;
}

typedef numerical_array<double> real_array;
typedef numerical_array<int> integer_array;

// class real_array : public numerical_array<double>
// {
// public:
// /// Constructs an empty array.
// real_array() : numerical_array<double>() {};
// typedef numerical_array<double> real_array;
// typedef numerical_array<int> integer_array;

class real_array : public numerical_array<double>
{
public:
/// Constructs an empty array.
real_array() : numerical_array<double>() {};

// /// Constructs an empty array with specified dimensions dims.
// real_array(std::vector<int> dims) : numerical_array<double>(dims) {};
/// Constructs an empty array with specified dimensions dims.
real_array(std::vector<int> dims) : numerical_array<double>(dims) {};

// /// Constructs an array with specified dimensions from scalars.
// real_array(std::vector<int> dims,std::vector<double> scalars) : numerical_array<double>(dims,scalars) {};
/// Constructs an array with specified dimensions from scalars.
real_array(std::vector<int> dims,std::vector<double> scalars) : numerical_array<double>(dims,scalars) {};
real_array(const numerical_array<double>& arr) : numerical_array<double>(arr) {};

// ~real_array() {};
~real_array() {};

// };
};

// class integer_array : public numerical_array<int>
// {
class integer_array : public numerical_array<int>
{
public:
/// Constructs an empty array.
integer_array() : numerical_array<int>() {};

/// Constructs an empty array with specified dimensions dims.
integer_array(std::vector<int> dims) : numerical_array<int>(dims) {};

// };
/// Constructs an array with specified dimensions from scalars.
integer_array(std::vector<int> dims,std::vector<int> scalars) : numerical_array<int>(dims,scalars) {};
integer_array(const numerical_array<int>& arr) : numerical_array<int>(arr) {};

// class boolean_array : public modelica_array<bool>
// {
integer_array(const modelica_array<int>& arr) : modelica_array<int>(arr) {};
// integer_array(const modelica_array<int>& arr) { m_dim_size = arr.m_dim_size;};

// };
~integer_array() {};

// class string_array : public modelica_array<std::string>
// {
/// Returns the n x n identity matrix.
friend integer_array identity_matrix(int n);

// };
/// Returns an array filled with zeros with dimensions given by dim.
friend integer_array zeros(std::vector<int> dims);

/// Returns an array filled with ones with dimensions given by dim.
friend integer_array ones(std::vector<int> dims);

};

integer_array identity_matrix(int n)
{
assert (n >= 1);

integer_array result(std::vector<int>(2,n));

for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
result.m_data[i*n + j] = (i==j) ? 1 : 0;
}
}
return result;
}

integer_array zeros(std::vector<int> dims)
{
integer_array result(dims);

for (int i = 0; i < result.nr_of_elements(); ++i)
{
result.m_data[i] = 0;
}
return result;
}

integer_array ones(std::vector<int> dims)
{
integer_array result(dims);

for (int i = 0; i < result.nr_of_elements(); ++i)
{
result.m_data[i] = 1;
}
return result;
}

class boolean_array : public modelica_array<bool>
{

};

class string_array : public modelica_array<std::string>
{

};


#endif

0 comments on commit 318d934

Please sign in to comment.