void Elem_face::calcK(Sparse& K){ if(!isInterior()) return; K.add(row,row,coef); K.add(col,col,coef); K.add(row,col,-coef); }
void initialize_sparse(Sparse& K){ Parent::initialize_sparse(K); for(int a=0;a<NUM_NODES;a++) for(int i=0;i<DIM;i++){ if(nodes[a]->dof[i].isDirichlet) continue; for(unsigned alpha=0;alpha<num_dof_p;alpha++) K.add(nodes[a]->dof[i].mi,m_pressure_id+alpha); } };
void Elem_face::initialize(Sparse& K){ if(!isInterior()) return; row=elem0->get_pressureID(); col=elem1->get_pressureID(); if(col<row){ int tmp=row; row=col; col=tmp; } K.add(row,col); };
const Sparse<T> operator/(const Sparse<T> &s, T b) { Vector<T> data(s.priv_data().size()); std::transform(s.priv_data().begin(), s.priv_data().end(), data.begin(), divided_constant<T,T>(b)); return Sparse<T>(s.dimensions(), s.priv_row_start(), s.priv_column(), data); }
const Sparse<T> operator-(const Sparse<T> &s) { Vector<T> data(s.priv_data().size()); std::transform(s.priv_data().begin(), s.priv_data().end(), data.begin(), std::negate<T>()); return Sparse<T>(s.dimensions(), s.priv_row_start(), s.priv_column(), data); }
void Sparse :: horzcat( const Sparse& A, const Sparse& B ) { assert( A.m == B.m ); assert( A.xtype == B.xtype ); data.clear(); m = A.m; n = A.n + B.n; xtype = A.xtype; for( const_iterator e = A.begin(); e != A.end(); e++ ) { int i = e->first.second; int j = e->first.first; data[ EntryIndex( j, i ) ] = e->second; } for( const_iterator e = B.begin(); e != B.end(); e++ ) { int i = e->first.second; int j = e->first.first; data[ EntryIndex( j+A.n, i ) ] = e->second; } }
void clear () { logger.info() << "Clearing structure" |0; Logging::IndentBlock block; basis.clear(); atoms.clear(); delete g_prior; delete g_parse_size; Assert (Dense::num_instances() == 0, "Dense instances remain after clearing structure"); o_size = a_size = c_size = j_size = 0; delete[] apps; apps = NULL; delete[] comps; comps = NULL; delete[] joins; joins = NULL; }
void DenseSubmatrix(Sparse<Scalar>& matrix, std::vector<int>& rows, std::vector<int>& cols, Dense<Scalar>& submatrix) { #ifndef RELEASE CallStackEntry entry("DenseSubmatrix"); #endif // TODO: avoid this copy Vector<int> iidx(rows.size()); for (size_t i = 0; i < rows.size(); ++i) { iidx.Set(i, rows[i]); } Vector<int> jidx(cols.size()); for (size_t j = 0; j < cols.size(); ++j) { jidx.Set(j, cols[j]); } submatrix.Resize(iidx.Size(), jidx.Size()); matrix.Find(iidx, jidx, submatrix); }
MatMxN GeodesicInHeat::compute_gradient(const Sparse& grad_x, const Sparse& grad_y, const Sparse& grad_z, const VecN& u) { //compute gradient on surface in x,y,z separatly int m=grad_x.rows(); VecN gradient_x = grad_x*u; VecN gradient_y = grad_y*u; VecN gradient_z = grad_z*u; MatMxN X(m, 3); X.col(0) = gradient_x; X.col(1) = gradient_y; X.col(2) = gradient_z; for (int i = 0; i != m; ++i) { X.row(i) =-X.row(i).normalized(); } return X; }
void calcK_UP(Sparse& K){ for(unsigned alpha=0;alpha<num_dof_p;alpha++) for(unsigned a=0;a<NUM_NODES;a++) for(unsigned i=0;i<DIM;i++){ if(nodes[a]->dof[i].isDirichlet) continue; int row=nodes[a]->dof[i].mi; //issue2 //K.add(row,m_pressure_id,m_dNdx[a][i]*m_Volume); double sum=0; for(unsigned gpi=0;gpi<NUM_GP;gpi++){ Default_gauss_point* gp=gauss_points[gpi]; sum+= gp->coor[alpha]*(m_J_rel_[gpi]+1.)*gp->dNdx[a][i]* gp->weight*gp->jacob; } K.add(row,m_pressure_id+alpha,sum); } };
namespace MeasLite { //WARNING: must match defs in .h file #define for_o for (Int o=1; o<=o_size; ++o) #define for_a for (const AEqn *e=a_begin, *end=a_end; e!=end; ++e) #define for_c for (const CEqn *e=c_begin, *end=c_end; e!=end; ++e) #define for_j for (const JEqn *e=j_begin, *end=j_end; e!=end; ++e) #define for_i(s) for (S::Iter i=s.begin(), end=s.end(); i!=end; ++i) //======== structure ======== Int o_size(0), a_size(0), c_size(0), j_size(0); const AEqn *apps = NULL, *a_begin = NULL, *a_end = NULL; const CEqn *comps = NULL, *c_begin = NULL, *c_end = NULL; const JEqn *joins = NULL, *j_begin = NULL, *j_end = NULL; Float P_app, P_comp, P_join, P_basis; Sparse basis("basis"); std::map<string,Ob> atoms; D* g_prior = NULL; D* g_parse_size = NULL; void init_prior (Float tol=1e-6); void init_parsing (); void load (string filename) { logger.info() << "Loading structure from " << filename |0; Logging::IndentBlock block; using namespace Files; //open file filename = filename + ".jdb"; FILE* file = fopen(filename.c_str(), "rb"); Assert (file, "failed to open file " << filename << " for loading"); //load and check header StructFileHeader header; header.load_from_file(file); start_validating(); header.validate(); Assert (everything_valid(), "invalid db file " << filename); //set params o_size = header.o_size; a_size = header.a_size; c_size = header.c_size; j_size = header.j_size; const Logging::fake_ostream& log = logger.info(); log << "loading " << o_size << " obs, " << a_size << " apps, " << c_size << " comps, " << j_size << " joins, " << header.w_size << " atoms." |1; //load app equations Assert ((apps = new(std::nothrow) AEqn[a_size]), "failed to allocate " << a_size << " equations"); fseek_block(file, header.a_data); safe_fread(const_cast<AEqn*>(apps), sizeof(AEqn), a_size, file); a_begin = apps; a_end = a_begin+a_size; log << "." |1; //--------------------------------------------------------- //load comp equations Assert ((comps = new(std::nothrow) CEqn[c_size]), "failed to allocate " << c_size << " equations"); fseek_block(file, header.c_data); safe_fread(const_cast<CEqn*>(comps), sizeof(CEqn), c_size, file); c_begin = comps; c_end = c_begin+c_size; log << "." |1; //--------------------------------------------------------- //load join equations Assert ((joins = new(std::nothrow) JEqn[j_size]), "failed to allocate " << j_size << " equations"); fseek_block(file, header.j_data); safe_fread(const_cast<JEqn*>(joins), sizeof(JEqn), j_size, file); j_begin = joins; j_end = j_begin+j_size; log << "." |1; //--------------------------------------------------------- //load basis //WARNING: must match Language::load_from_file in languages.C IntWMass* weights = new IntWMass[header.w_size]; fseek_block(file, header.L_data); safe_fread(&P_app, sizeof(Float), 1, file); safe_fread(&P_comp, sizeof(Float), 1, file); safe_fread(&P_join, sizeof(Float), 1, file); P_basis = 1.0 - P_app - P_comp - P_join; safe_fread(weights, sizeof(IntWMass), header.w_size, file); basis.data.insert(weights, weights+header.w_size); Assert (basis.data.size() == header.w_size, "wrong number of atoms: " << basis.data.size()) delete[] weights; basis.validate(); log << "." |1; //--------------------------------------------------------- //load atom names //WARNING: must match Brain::load(filename) fseek_block(file, header.b_data); unsigned N = header.b_size; //WARNING: must match CombinatoryStructure::load_from_file(filename,b_size) ObNamePair* names = new ObNamePair[N]; safe_fread(names, sizeof(ObNamePair), N, file); for (Int n=0; n<N; ++n) { string name = names[n].name; //logger.debug() << "loading atom " << name |0; atoms[name] = names[n].ob; } Assert (atoms.size() == N, "duplicated names in loading atoms"); delete[] names; log << "." |1; //--------------------------------------------------------- fclose(file); log << "done" |0; //init globals g_prior = new D("prior"); g_parse_size = new D("parse_size"); init_prior (); init_parsing (); } void clear () { logger.info() << "Clearing structure" |0; Logging::IndentBlock block; basis.clear(); atoms.clear(); delete g_prior; delete g_parse_size; Assert (Dense::num_instances() == 0, "Dense instances remain after clearing structure"); o_size = a_size = c_size = j_size = 0; delete[] apps; apps = NULL; delete[] comps; comps = NULL; delete[] joins; joins = NULL; } //======== parsing + printing ======== Ob parse (std::string expr) { TODO(); } std::string print (Ob ob) { TODO(); } void init_parse_size(); void init_parsing () { logger.info() << "Initializing parsing" |0; Logging::IndentBlock block; init_parse_size(); //TODO parse terms as strings } //======== measures ======== unsigned Dense::s_num_instances = 0; //low-level operations Float D::total () { double tot = 0; for_o tot += m.data[o]; return tot; }; Float S::total () { Float tot = 0; for (Sparse::CIter i=m.begin(); i!=m.end(); ++i) tot += i->second; return tot; } inline Float h_term (Float t) { return t > 0 ? t * logf(t) : 0; } Float D::entropy () { double h = 0; for_o h += h_term(m.data[o]); return - h; }; Float S::entropy () { Float h = 0; for (Sparse::CIter i=m.begin(); i!=m.end(); ++i) h += h_term(i->second); return - h; } void Sparse::validate () { for (CIter i=data.begin(); i!=data.end(); ++i) { Ob o = i->first; Assert (1 <= o and o <= o_size, "ob out of range: " << o); } } //======== random generators ======== inline Float random_unif () { return drand48(); } Float random_normal () {//box-muller static bool available = true; available = not available; //x,y are normally distrubuted static Float y = 0.0f; if (available) return y; Float theta = 2.0f * M_PI * random_unif(); Float r = sqrtf(-2.0f * logf(1.0f - random_unif())); Float x = r * cosf(theta); y = r * sinf(theta); return x; } //======== complexity calculations ======== void init_prior (Float tol) { logger.info() << "Initializing complexity prior" |0; Logging::IndentBlock block; //variables D c(prior()); S c0(basis); //calculation c = c0; c = fix( P_basis * c0 + P_app * app(c,c) + P_comp * comp(c,c) + P_join * join(c,c) ); } void init_parse_size () { logger.info() << "Initializing parsing" |0; Logging::IndentBlock block; //variables D k(parse_size()); S k0(basis); //set size of atoms k = P_basis * k0; //iteratively set size of compounds for (bool done = true; not done; done = true) { //set size of apps for (const AEqn *e=a_begin, *end=a_end; e!=end; ++e) { Float &k_old = k.data(e->a); Float k_new = P_app * k[e->l] * k[e->r]; if (k_new < k_old) { k_new = k_old; done = false; } } //set size of comps for (const CEqn *e=c_begin, *end=c_end; e!=end; ++e) { Float &k_old = k.data(e->c); Float k_new = P_comp * k[e->l] * k[e->r]; if (k_new < k_old) { k_new = k_old; done = false; } } //set size of joins for (const JEqn *e=j_begin, *end=j_end; e!=end; ++e) { Float &k_old = k.data(e->j); Float k_new = P_join * k[e->l] * k[e->r]; if (k_new < k_old) { k_new = k_old; done = false; } } } } }
void load (string filename) { logger.info() << "Loading structure from " << filename |0; Logging::IndentBlock block; using namespace Files; //open file filename = filename + ".jdb"; FILE* file = fopen(filename.c_str(), "rb"); Assert (file, "failed to open file " << filename << " for loading"); //load and check header StructFileHeader header; header.load_from_file(file); start_validating(); header.validate(); Assert (everything_valid(), "invalid db file " << filename); //set params o_size = header.o_size; a_size = header.a_size; c_size = header.c_size; j_size = header.j_size; const Logging::fake_ostream& log = logger.info(); log << "loading " << o_size << " obs, " << a_size << " apps, " << c_size << " comps, " << j_size << " joins, " << header.w_size << " atoms." |1; //load app equations Assert ((apps = new(std::nothrow) AEqn[a_size]), "failed to allocate " << a_size << " equations"); fseek_block(file, header.a_data); safe_fread(const_cast<AEqn*>(apps), sizeof(AEqn), a_size, file); a_begin = apps; a_end = a_begin+a_size; log << "." |1; //--------------------------------------------------------- //load comp equations Assert ((comps = new(std::nothrow) CEqn[c_size]), "failed to allocate " << c_size << " equations"); fseek_block(file, header.c_data); safe_fread(const_cast<CEqn*>(comps), sizeof(CEqn), c_size, file); c_begin = comps; c_end = c_begin+c_size; log << "." |1; //--------------------------------------------------------- //load join equations Assert ((joins = new(std::nothrow) JEqn[j_size]), "failed to allocate " << j_size << " equations"); fseek_block(file, header.j_data); safe_fread(const_cast<JEqn*>(joins), sizeof(JEqn), j_size, file); j_begin = joins; j_end = j_begin+j_size; log << "." |1; //--------------------------------------------------------- //load basis //WARNING: must match Language::load_from_file in languages.C IntWMass* weights = new IntWMass[header.w_size]; fseek_block(file, header.L_data); safe_fread(&P_app, sizeof(Float), 1, file); safe_fread(&P_comp, sizeof(Float), 1, file); safe_fread(&P_join, sizeof(Float), 1, file); P_basis = 1.0 - P_app - P_comp - P_join; safe_fread(weights, sizeof(IntWMass), header.w_size, file); basis.data.insert(weights, weights+header.w_size); Assert (basis.data.size() == header.w_size, "wrong number of atoms: " << basis.data.size()) delete[] weights; basis.validate(); log << "." |1; //--------------------------------------------------------- //load atom names //WARNING: must match Brain::load(filename) fseek_block(file, header.b_data); unsigned N = header.b_size; //WARNING: must match CombinatoryStructure::load_from_file(filename,b_size) ObNamePair* names = new ObNamePair[N]; safe_fread(names, sizeof(ObNamePair), N, file); for (Int n=0; n<N; ++n) { string name = names[n].name; //logger.debug() << "loading atom " << name |0; atoms[name] = names[n].ob; } Assert (atoms.size() == N, "duplicated names in loading atoms"); delete[] names; log << "." |1; //--------------------------------------------------------- fclose(file); log << "done" |0; //init globals g_prior = new D("prior"); g_parse_size = new D("parse_size"); init_prior (); init_parsing (); }
bool getDimsFromArguments(types::typed_list& in, const std::string& _pstName, int* _iDims, int** _piDims, bool* _alloc) { types::Double* pOut = 0; *_alloc = false; *_iDims = 0; *_piDims = NULL; if (in.size() == 0) { *_iDims = 2; *_piDims = new int[*_iDims]; (*_piDims)[0] = 1; (*_piDims)[1] = 1; *_alloc = true; return true; } else if (in.size() == 1) { *_iDims = 1; // : if (in[0]->isColon()) { *_iDims = -1; return false; } if (in[0]->isArrayOf() == false) { if (in[0]->isSparse()) { Sparse* sp = in[0]->getAs<Sparse>(); *_iDims = sp->getDims(); *_piDims = sp->getDimsArray(); return true; } else if (in[0]->isSparseBool()) { SparseBool* sp = in[0]->getAs<SparseBool>(); *_iDims = sp->getDims(); *_piDims = sp->getDimsArray(); return true; } return false; } GenericType* pIn = in[0]->getAs<GenericType>(); *_iDims = pIn->getDims(); *_piDims = pIn->getDimsArray(); return true; } else { *_iDims = static_cast<int>(in.size()); *_piDims = new int[*_iDims]; *_alloc = true; for (int i = 0; i < *_iDims; i++) { if (in[i]->isArrayOf() == false) { delete[] * _piDims; Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1); return false; } types::GenericType* pGTIn = in[i]->getAs<types::GenericType>(); if (pGTIn->isScalar() == false || pGTIn->isComplex()) { delete[] * _piDims; Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1); return false; } switch (in[i]->getType()) { case types::InternalType::ScilabDouble: { double dValue = in[i]->getAs<types::Double>()->get(0); if (dValue >= INT_MAX) { delete[] * _piDims; Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX); return false; } (*_piDims)[i] = static_cast<int>(dValue); } break; case types::InternalType::ScilabInt8: (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int8>()->get()[0]); break; case types::InternalType::ScilabUInt8: (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt8>()->get()[0]); break; case types::InternalType::ScilabInt16: (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int16>()->get()[0]); break; case types::InternalType::ScilabUInt16: (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt16>()->get()[0]); break; case types::InternalType::ScilabInt32: (*_piDims)[i] = in[i]->getAs<types::Int32>()->get()[0]; break; case types::InternalType::ScilabUInt32: (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt32>()->get()[0]); break; case types::InternalType::ScilabInt64: { long long llValue = in[i]->getAs<types::Int64>()->get(0); if (llValue >= INT_MAX) { delete[] * _piDims; Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX); return false; } (*_piDims)[i] = static_cast<int>(llValue); break; } case types::InternalType::ScilabUInt64: { unsigned long long ullValue = in[i]->getAs<types::UInt64>()->get(0); if (ullValue >= INT_MAX) { delete[] * _piDims; Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX); return false; } (*_piDims)[i] = static_cast<int>(ullValue); break; } default: Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1); return false; } } return true; } return false; }
const Sparse<T> sparse_binop(const Sparse<T> &m1, const Sparse<T> &m2, binop op) { size_t rows = m1.rows(); size_t cols = m1.columns(); assert(rows == m2.rows() && cols == m2.columns()); if (rows == 0 || cols == 0) return m1; index max_size = m1.priv_data().size() + m2.priv_data().size(); Vector<T> data(max_size); Indices column(max_size); Indices row_start(rows + 1); typename Vector<T>::iterator out_data = data.begin(); typename Indices::iterator out_column = column.begin(); typename Indices::iterator out_row_start = row_start.begin(); typename Vector<T>::iterator out_begin = out_data; typename Vector<T>::const_iterator m1_data = m1.priv_data().begin(); typename Indices::const_iterator m1_row_start = m1.priv_row_start().begin(); typename Indices::const_iterator m1_column = m1.priv_column().begin(); typename Vector<T>::const_iterator m2_data = m2.priv_data().begin(); typename Indices::const_iterator m2_row_start = m2.priv_row_start().begin(); typename Indices::const_iterator m2_column = m2.priv_column().begin(); index j1 = *(m1_row_start++); // data start for this row in M1 index l1 = (*m1_row_start) - j1; // # elements in this row in M1 index j2 = *(m2_row_start++); // data start for this row in M2 index l2 = (*m2_row_start) - j2; // # elements in this row in M2 *out_row_start = 0; while (1) { // We look for the next unprocessed matrix element on this row, // for both matrices. c1 and c2 are the columns associated to // each element on each matrix. index c1 = l1 ? *m1_column : cols; index c2 = l2 ? *m2_column : cols; T value; index c; if (c1 < c2) { // There is an element a column c1 on matrix m1, but the // same element at m2 is zero value = op(*m1_data, number_zero<T>()); c = c1; l1--; m1_column++; m1_data++; } else if (c2 < c1) { // There is an element a column c2 on matrix m2, but the // same element at m1 is zero value = op(number_zero<T>(), *m2_data); c = c2; l2--; m2_column++; m2_data++; } else if (c2 < cols) { // Both elements in m1 and m2 are nonzero. value = op(*m1_data, *m2_data); c = c1; l1--; l2--; m1_column++; m1_data++; m2_column++; m2_data++; } else { // We have processed all elements in this row. out_row_start++; *out_row_start = out_data - out_begin; if (--rows == 0) { break; } j1 = *m1_row_start; m1_row_start++; l1 = (*m1_row_start) - j1; j2 = *m2_row_start; m2_row_start++; l2 = (*m2_row_start) - j2; continue; } if (!(value == number_zero<T>())) { *(out_data++) = value; *(out_column++) = c; } } index j = out_data - out_begin; Indices the_column(j); std::copy(column.begin(), column.begin() + j, the_column.begin()); Vector<T> the_data(j); std::copy(data.begin(), data.begin() + j, the_data.begin()); return Sparse<T>(m1.dimensions(), row_start, the_column, the_data); }
int RDivideSparseByDouble(types::Sparse* _pSp, types::Double* _pDouble, InternalType** _pSpOut) { if (_pDouble->isEmpty()) { //sp / [] *_pSpOut = Double::Empty(); return 0; } if (_pDouble->isIdentity()) { *_pSpOut = new Sparse(*_pSp); return 0; } size_t iSize = _pSp->nonZeros(); int* Col = new int[iSize]; int* Row = new int[_pSp->getRows()]; _pSp->getColPos(Col); _pSp->getNbItemByRow(Row); int* iPositVal = new int[iSize]; int idx = 0; for (int i = 0; i < _pSp->getRows(); i++) { for (int j = 0; j < Row[i]; j++) { iPositVal[idx] = (Col[idx] - 1) * _pSp->getRows() + i; ++idx; } } Double** pDbl = new Double*[iSize]; Double** pDblSp = new Double*[iSize]; double* pdbl = _pDouble->get(); if (_pDouble->isScalar()) { if (_pDouble->isComplex()) { double* pdblImg = _pDouble->getImg(); for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[0], pdblImg[0]); pDblSp[i] = new Double(_pSp->get(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } else { for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[0]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } } else if (_pDouble->getSize() == iSize) { if (_pDouble->isComplex()) { double* pdblImg = _pDouble->getImg(); for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[i], pdblImg[i]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } else { for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[i]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } } else { for (int i = 0; i < iSize; ++i) { delete pDbl[i]; delete pDblSp[i]; } delete[] pDbl; delete[] pDblSp; throw ast::InternalError(_W("Invalid exponent.\n")); return 1; } Sparse* pSpTemp = new Sparse(_pSp->getRows(), _pSp->getCols(), _pSp->isComplex() || _pDouble->isComplex()); pSpTemp->zero_set(); Double* ppDblGet = NULL; int iResultat; for (int i = 0; i < iSize; i++) { if ((pDblSp[i]->get(0) != 0) || (pDblSp[i]->getImg(0) != 0)) { iResultat = RDivideDoubleByDouble(pDblSp[i], pDbl[i], &ppDblGet); if (iResultat != 0) { delete ppDblGet; return iResultat; } std::complex<double> cplx(ppDblGet->get(0), ppDblGet->getImg(0)); pSpTemp->set(iPositVal[i], cplx, true); delete ppDblGet; } } delete[] Col; delete[] Row; delete[] iPositVal; for (int i = 0; i < iSize; ++i) { delete pDbl[i]; delete pDblSp[i]; } delete[] pDbl; delete[] pDblSp; *_pSpOut = pSpTemp; return 0; }
int DotPowerSpaseByDouble(Sparse* _pSp, Double* _pDouble, InternalType** _pOut) { if (_pDouble->isEmpty()) { //sp .^ [] *_pOut = Double::Empty(); return 0; } size_t iSize = _pSp->nonZeros(); int* Col = new int[iSize]; int* Row = new int[iSize]; _pSp->getColPos(Col); _pSp->getNbItemByRow(Row); int* iPositVal = new int[iSize]; int j = 0; for (int i = 0; i < iSize; j++) { for (int k = 0; k < Row[j]; k++) { iPositVal[i] = (Col[i] - 1) * _pSp->getRows() + j; i++; } } Double** pDbl = new Double*[iSize]; Double** pDblSp = new Double*[iSize]; double* pdbl = _pDouble->get(); if (_pDouble->isScalar()) { if (_pDouble->isComplex()) { double* pdblImg = _pDouble->getImg(); for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[0], pdblImg[0]); pDblSp[i] = new Double(_pSp->get(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } else { for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[0]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } } else if (_pDouble->getSize() == iSize) { if (_pDouble->isComplex()) { double* pdblImg = _pDouble->getImg(); for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[i], pdblImg[i]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } else { for (int i = 0; i < iSize; i++) { pDbl[i] = new Double(pdbl[i]); pDblSp[i] = new Double(_pSp->getReal(iPositVal[i]), _pSp->getImg(iPositVal[i]).imag()); } } } else { delete[] pDblSp; throw ast::InternalError(_W("Invalid exponent.\n")); return 1; } Sparse* pSpTemp = new Sparse(_pSp->getRows(), _pSp->getCols(), _pSp->isComplex() || _pDouble->isComplex()); pSpTemp->zero_set(); Double* ppDblGet = NULL; for (int i = 0; i < iSize; i++) { if ((pDblSp[i]->get(0) != 0) || (pDblSp[i]->getImg(0) != 0)) { DotPowerDoubleByDouble(pDblSp[i], pDbl[i], &ppDblGet); std::complex<double> cplx(ppDblGet->get(0), ppDblGet->getImg(0)); pSpTemp->set(iPositVal[i], cplx, false); } } delete[] Col; delete[] Row; delete[] iPositVal; pSpTemp->finalize(); *_pOut = pSpTemp; return 0; }
static const Sparse<elt_t> do_kron(const Sparse<elt_t> &s2, const Sparse<elt_t> &s1) { index rows1 = s1.rows(); index cols1 = s1.columns(); index rows2 = s2.rows(); index cols2 = s2.columns(); index number_nonzero = s1.length() * s2.length(); index total_rows = rows1 * rows2; index total_cols = cols1 * cols2; if (number_nonzero == 0) return Sparse<elt_t>(total_rows, total_cols); Tensor<elt_t> output_data(number_nonzero); Indices output_column(number_nonzero); Indices output_row_start(total_rows+1); Indices output_dims(igen << total_rows << total_cols); typename Tensor<elt_t>::iterator out_data = output_data.begin(); typename Indices::iterator out_column = output_column.begin(); typename Indices::iterator out_begin = out_column; typename Indices::iterator out_row_start = output_row_start.begin(); // C([i,j],[k,l]) = s1(i,k) s2(j,l) *(out_row_start++) = 0; for (index l = 0; l < rows2; l++) { for (index k = 0; k < rows1; k++) { for (index j = s2.priv_row_start()[l]; j < s2.priv_row_start()[l+1]; j++) { for (index i = s1.priv_row_start()[k]; i < s1.priv_row_start()[k+1]; i++) { *(out_data++) = s1.priv_data()[i] * s2.priv_data()[j]; *(out_column++) = s1.priv_column()[i] + cols1 * s2.priv_column()[j]; } } *(out_row_start++) = out_column - out_begin; } } return Sparse<elt_t>(output_dims, output_row_start, output_column, output_data); }