NMGenerator::Type type(const nvar& v){ switch(v.type()){ case nvar::Function:{ NMGenerator::Type t = NMGenerator::type(v); if(!(t & NMGenerator::Supported) || t == NMGenerator::Requested){ return t; } size_t size = v.size(); for(size_t i = 0; i < size; ++i){ NMGenerator::Type ti = type(v[i]); if(!(ti & NMGenerator::Supported)){ return ti; } else if(ti == NMGenerator::Requested){ t = ti; } } return t; } case nvar::Symbol: return o_->Get(v).isNone() ? NMGenerator::Requested : NMGenerator::Supported; } return NMGenerator::Supported; }
void setStringMapAttribute_(const nstr& key, const nvar& value){ nvec values; if(value.isString()){ values.push_back(value); } else{ if(!value.hasVector()){ error_(key, value); } for(size_t i = 0; i < value.size(); ++i){ if(!value[i].isString()){ error_(key, value); } values.push_back(value[i]); } } nvar& r = attrs_(key); for(const nstr& k : values){ r(k) = true; } }
nvar product(const nvar& v){ nvar ret = multiplier_; for(size_t i = 0; i < v.size(); ++i){ ret *= v[i]; } return ret; }
void setStringAttribute_(const nstr& key, const nvar& value){ nstr str; if(value.isString()){ str = value; } else{ if(!value.hasVector()){ error_(key, value); } for(size_t i = 0; i < value.size(); ++i){ if(!value[i].isString()){ error_(key, value); } } str = nstr::join(value.vec(), "; "); } attrs_(key) = str; }
void NMParser_::translate(nvar& n){ if(n.isSymbol()){ SymbolMap::const_iterator itr = _symbolMap.find(n.str()); SymbolKey key = itr->second; switch(key){ case SKEY_True: n = true; break; case SKEY_False: n = false; break; case SKEY_E: n.str() = "Eu"; break; default: break; } return; } size_t size = n.size(); FunctionMap::const_iterator itr = _functionMap.find({n.str(), size}); if(itr == _functionMap.end()){ itr = _functionMap.find({n.str(), -1}); } if(itr == _functionMap.end()){ error(n, "unrecognized function: " + n); return; } FunctionKey key = itr->second; switch(key){ case FKEY_Set_2: case FKEY_Mod_2: case FKEY_Not_1: case FKEY_And_2: case FKEY_Or_2: case FKEY_Exp_1: case FKEY_Sqrt_1: case FKEY_Abs_1: case FKEY_Log_1: case FKEY_Cos_1: case FKEY_Sin_1: case FKEY_Sinh_1: case FKEY_Tan_1: case FKEY_Tanh_1: case FKEY_Integrate_2: case FKEY_GCD_n: case FKEY_LCM_n: case FKEY_Factorial_1: return; case FKEY_Times_n: if(size < 2){ break; } n.str() = "Mul"; n.foldRight(); return; case FKEY_Plus_n: if(size < 2){ break; } n.str() = "Add"; n.foldRight(); return; case FKEY_Power_2: n.str() = "Pow"; return; case FKEY_Rational_2: n = nrat(n[0], n[1]); return; case FKEY_Less_2: n.str() = "LT"; return; case FKEY_Greater_2: n.str() = "GT"; return; case FKEY_LessEqual_2: n.str() = "LE"; return; case FKEY_GreaterEqual_2: n.str() = "GE"; return; case FKEY_Equal_2: n.str() = "EQ"; return; case FKEY_Unequal_2: n.str() = "NE"; return; case FKEY_Increment_1: n.str() = "PostInc"; return; case FKEY_PreIncrement_1: n.str() = "Inc"; return; case FKEY_Decrement_1: n.str() = "PostDec"; return; case FKEY_PreDecrement_1: n.str() = "Dec"; return; case FKEY_ArcCos_1: n.str() = "Acos"; return; case FKEY_Cosh_1: n.str() = "Cosh"; return; case FKEY_ArcSin_1: n.str() = "Asin"; return; case FKEY_ArcTan_1: n.str() = "Atan"; return; case FKEY_D_2: n.str() = "Derivative"; return; case FKEY_List_n:{ size_t size = n.size(); nvar v; for(size_t i = 0; i < size; ++i){ v << move(n[i]); } n = move(v); return; } } error(n, "unrecognized function: " + n); }