Skip to content

Commit

Permalink
added mnissed constructor for SplineVec improved constructor for Spli…
Browse files Browse the repository at this point in the history
…neSet
  • Loading branch information
ebertolazzi committed Oct 14, 2024
1 parent cee6124 commit 0f9bd68
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 158 deletions.
199 changes: 116 additions & 83 deletions src/SplineSetGC.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace Splines {

void
SplineSet::setup( GenericContainer const & gc ) {

string where{ fmt::format( "SplineSet[{}]::setup( gc ): ", m_name ) };

/*
// gc["spline_type"]
// gc["xdata"]
Expand All @@ -60,26 +63,25 @@ namespace Splines {
vec_string_type headers;
vector<vec_real_type> Y, Yp;

string where{ fmt::format( "SplineSet[{}]::setup( gc ): ", m_name ) };
GenericContainer const & gc_stype{ gc("spline_type",where.c_str()) };
GenericContainer const & gc_xdata{ gc("xdata",where.c_str()) };
GenericContainer const & gc_ydata{ gc("ydata",where.c_str()) };

gc("spline_type",where.c_str()).copyto_vec_string(
//
// gc["spline_type"]
//
gc_stype.copyto_vec_string(
spline_type_vec, (where+"in reading `spline_type'\n").c_str()
);
m_nspl = integer(spline_type_vec.size());
stype.resize( size_t(m_nspl) );
for ( size_t spl{0}; spl < size_t(m_nspl); ++spl )
stype[spl] = string_to_splineType1D( spline_type_vec[spl] );

gc("xdata",where.c_str()).copyto_vec_real( X, (where+"reading `xdata'").c_str() );
m_npts = integer( X.size() );

GenericContainer const & gc_ydata{ gc("ydata",where.c_str()) };

// allocate for _nspl splines
Y . resize( size_t(m_nspl) );
Yp . resize( size_t(m_nspl) );

// se tipo vettore o matrice deve esserci headers
// se non tipo MAP deve esserci headers
//
// gc["headers"] (opzionale)
//
if ( GC_type::MAP != gc_ydata.get_type() ) {
GenericContainer const & gc_headers{ gc("headers",where.c_str()) };
gc_headers.copyto_vec_string( headers, (where+", reading `headers'").c_str() );
Expand All @@ -90,88 +92,119 @@ namespace Splines {
);
}

if ( GC_type::VEC_REAL == gc_ydata.get_type() ) {
// leggo vecttore come matrice di una sola colonna
vec_real_type const & data{ gc_ydata.get_vec_real() };
UTILS_ASSERT(
m_nspl == 1,
"{}, number of splines [{}]\n"
"is incompatible with the type of `ydata` a vector (or a matrix with 1 column) in data\n",
where, m_nspl
);
UTILS_ASSERT(
size_t(m_npts) == data.size(),
"{}, number of points [{}]\n"
"differs from the number of `ydata` rows [{}] in data\n",
where, m_npts, data.size()
);
Y[0].reserve(data.size());
std::copy( data.begin(), data.end(), std::back_inserter(Y[0]) );
} else if ( GC_type::MAT_REAL == gc_ydata.get_type() ) {
// leggo matrice
mat_real_type const & data{ gc_ydata.get_mat_real() };
UTILS_ASSERT(
size_t(m_nspl) == data.numCols(),
"{}, number of splines [{}]\n"
"differs from the number of `ydata` columns [{}] in data\n",
where, m_nspl, data.numCols()
);
UTILS_ASSERT(
size_t(m_npts) == data.numRows(),
"{}, number of points [{}]\n"
"differs from the number of `ydata` rows [{}] in data\n",
where, m_npts, data.numRows()
);
for ( size_t i{0}; i < size_t(m_nspl); ++i )
data.getColumn(unsigned(i),Y[i]);
} else if ( GC_type::VECTOR == gc_ydata.get_type() ) {
vector_type const & data{ gc_ydata.get_vector() };
UTILS_ASSERT(
size_t(m_nspl) == data.size(),
"{}, field `ydata` expected of size {} found of size {}\n",
where, m_nspl, data.size()
);
string msg1{ where+" reading `ydata` columns" };
for ( size_t spl = 0; spl < size_t(m_nspl); ++spl ) {
GenericContainer const & datai{ data[spl] };
integer nrow{ m_npts };
if ( stype[spl] == SplineType1D::CONSTANT ) --nrow; // constant spline uses n-1 points
datai.copyto_vec_real( Y[spl], msg1.c_str() );
//
// gc["xdata"]
//
gc_xdata.copyto_vec_real( X, (where+"reading `xdata'").c_str() );
m_npts = integer( X.size() );

// allocate for _nspl splines
Y . resize( size_t(m_nspl) );
Yp . resize( size_t(m_nspl) );

switch ( gc_ydata.get_type() ) {
case GC_type::VEC_BOOL:
case GC_type::VEC_INTEGER:
case GC_type::VEC_LONG:
case GC_type::VEC_REAL:
case GC_type::VEC_COMPLEX:
{
// leggo vecttore come matrice di una sola colonna
vec_real_type data;
gc_ydata.copyto_vec_real( data, (where+"reading `ydata'").c_str() );
UTILS_ASSERT(
size_t(nrow) == Y[spl].size(),
"{}, column {} of `ydata` of type `{}` expected of size {} found of size {}\n",
where, spl, spline_type_vec[spl], nrow, Y[spl].size()
m_nspl == 1,
"{}, number of splines [{}]\n"
"is incompatible with the type of `ydata` a vector (or a matrix with 1 column) in data\n",
where, m_nspl
);
UTILS_ASSERT(
size_t(m_npts) == data.size(),
"{}, number of points [{}]\n"
"differs from the number of `ydata` rows [{}] in data\n",
where, m_npts, data.size()
);
Y[0].reserve(data.size());
std::copy( data.begin(), data.end(), std::back_inserter(Y[0]) );
}
} else if ( GC_type::MAP == gc_ydata.get_type() ) {
map_type const & data{ gc_ydata.get_map() };
UTILS_ASSERT(
data.size() == size_t(m_nspl),
"{}, field `ydata` expected of size {} found of size {}\n",
where, m_nspl, data.size()
);
headers.clear(); headers.reserve(data.size());
map_type::const_iterator im{ data.begin() };
string msg1{ where+" reading `ydata` columns" };
for ( size_t spl{0}; im != data.end(); ++im, ++spl ) {
headers.push_back(im->first);
GenericContainer const & datai{ im->second };
integer nrow{ m_npts };
if ( stype[spl] == SplineType1D::CONSTANT ) --nrow; // constant spline uses n-1 points
datai.copyto_vec_real( Y[spl], msg1.c_str() );
break;
case GC_type::MAT_INTEGER:
case GC_type::MAT_LONG:
case GC_type::MAT_REAL:
{
mat_real_type data;
gc_ydata.copyto_mat_real( data, (where+"reading `ydata'").c_str() );
UTILS_ASSERT(
size_t(nrow) == Y[spl].size(),
"{}, column `{}` of `ydata` ot type `{}` expected of size {} found of size {}\n",
where, im->first, spline_type_vec[spl], nrow, Y[spl].size()
size_t(m_nspl) == data.num_cols(),
"{}, number of splines [{}]\n"
"differs from the number of `ydata` columns [{}] in data\n",
where, m_nspl, data.num_cols()
);
UTILS_ASSERT(
size_t(m_npts) == data.num_rows(),
"{}, number of points [{}]\n"
"differs from the number of `ydata` rows [{}] in data\n",
where, m_npts, data.num_rows()
);
for ( size_t i{0}; i < size_t(m_nspl); ++i )
data.get_column(unsigned(i),Y[i]);
}
break;
case GC_type::VECTOR:
{
vector_type const & data{ gc_ydata.get_vector() };
UTILS_ASSERT(
size_t(m_nspl) == data.size(),
"{}, field `ydata` expected of size {} found of size {}\n",
where, m_nspl, data.size()
);
string msg1{ where+" reading `ydata` columns" };
for ( size_t spl = 0; spl < size_t(m_nspl); ++spl ) {
GenericContainer const & datai{ data[spl] };
integer nrow{ m_npts };
if ( stype[spl] == SplineType1D::CONSTANT ) --nrow; // constant spline uses n-1 points
datai.copyto_vec_real( Y[spl], msg1.c_str() );
UTILS_ASSERT(
size_t(nrow) == Y[spl].size(),
"{}, column {} of `ydata` of type `{}` expected of size {} found of size {}\n",
where, spl, spline_type_vec[spl], nrow, Y[spl].size()
);
}
}
break;
case GC_type::MAP:
{
map_type const & data{ gc_ydata.get_map() };
UTILS_ASSERT(
data.size() == size_t(m_nspl),
"{}, field `ydata` expected of size {} found of size {}\n",
where, m_nspl, data.size()
);
headers.clear(); headers.reserve(data.size());
map_type::const_iterator im{ data.begin() };
string msg1{ where+" reading `ydata` columns" };
for ( size_t spl{0}; im != data.end(); ++im, ++spl ) {
headers.push_back(im->first);
GenericContainer const & datai{ im->second };
integer nrow{ m_npts };
if ( stype[spl] == SplineType1D::CONSTANT ) --nrow; // constant spline uses n-1 points
datai.copyto_vec_real( Y[spl], msg1.c_str() );
UTILS_ASSERT(
size_t(nrow) == Y[spl].size(),
"{}, column `{}` of `ydata` ot type `{}` expected of size {} found of size {}\n",
where, im->first, spline_type_vec[spl], nrow, Y[spl].size()
);
}
}
} else {
break;
default:
UTILS_ERROR(
"{}, field `data` expected\n"
"to be of type `mat_real_type`, `vector_type` or `map_type'\n"
"to be of type `vec_[int/long/real]_type`, `mat_[int/long/real]_type`, `vector_type` or `map_type'\n"
"found: `{}`\n",
where, gc_ydata.get_type_name()
);
break;
}

if ( gc.exists("ypdata") ) { // yp puo' essere solo tipo map
Expand Down
32 changes: 27 additions & 5 deletions src/SplineVec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,32 @@ namespace Splines {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void
SplineVec::setup( GenericContainer const & ) {
//allocate( integer dim, integer npts );
// DA COMPLETARE
UTILS_ERROR("SplineVec::setup not yet implemented");
SplineVec::setup( GenericContainer const & gc ) {

string where{ fmt::format("SplineVec[{}]::setup( gc ):", m_name ) };

GenericContainer const & data = gc("data",where.c_str());

mat_real_type Y;
data.copyto_mat_real(Y,where.c_str());

bool transposed{false};
gc.get_if_exists("transposed",transposed);

if ( transposed ) {
integer dim { integer(Y.num_rows()) };
integer npts{ integer(Y.num_cols()) };
allocate( dim, npts );
for ( size_t spl{0}; spl < size_t(m_dim); ++spl )
for ( size_t j{0}; j < size_t(m_npts); ++j )
m_Y[spl][j] = Y(spl,j);
} else {
integer dim { integer(Y.num_cols()) };
integer npts{ integer(Y.num_rows()) };
allocate( dim, npts );
for ( size_t spl{0}; spl < size_t(m_dim); ++spl )
for ( size_t j{0}; j < size_t(m_npts); ++j )
m_Y[spl][j] = Y(j,spl);
}
}

}
4 changes: 2 additions & 2 deletions src/Splines/SplineVec.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ namespace Splines {
//! Initialize the interpolation point of the splines.
//!
//! \param[in] dim the dimension of the points
//! \param[in] npts the numeber of interpolation points
//! \param[in] npts the number of interpolation points
//! \param[in] Y the matrix of points values, `Y[i]` is the
//! pointer to the i-th components
//!
Expand All @@ -509,7 +509,7 @@ namespace Splines {
//! Initialize the interpolation point of the splines.
//!
//! \param[in] dim the dimension of the points
//! \param[in] npts the numeber of interpolation points
//! \param[in] npts the number of interpolation points
//! \param[in] Y the matrix of points values
//! \param[in] ldY leading dimension of the matrix
//!
Expand Down
Loading

0 comments on commit 0f9bd68

Please sign in to comment.