Number* Rational::acoss() { Float* res = new Float; Float* tmp = SCAST_FLOAT(res->convert(this)); if (fabs(tmp->number_)<=1.0) { res->number_=asin(tmp->number_); delete tmp; return res; } else if (tmp->number_>0) { complex<double> c_a(tmp->number_,0.0); complex<double> c_res = asin(c_a); Complex* res = new Complex; res->exact_ = false; res->real_ = new Float(::real(c_res)); res->imag_ = new Float(::imag(c_res)); delete tmp; return res; } else { complex<double> c_a(fabs(tmp->number_),0.0); complex<double> c_res = asin(c_a); Complex* res = new Complex; res->exact_ = false; res->real_ = new Float(::real(c_res)+PI); res->imag_ = new Float(0.0-::imag(c_res)); delete tmp; return res; } }
double capi_rfloat_value(VALUE flt) { NativeMethodEnvironment* env = NativeMethodEnvironment::get(); Handle* handle = Handle::from(flt); Float* f = c_as<Float>(handle->object()); return f->to_double(env->state()); }
void flush_cached_rfloat(NativeMethodEnvironment* env, Handle* handle) { if(handle->is_rfloat()) { Float* obj = c_as<Float>(handle->object()); RFloat* rfloat = handle->as_rfloat(env); obj->value(rfloat->value); } }
void LocationMapForm::HandleJavaScriptRequestN (Tizen::Web::Json::IJsonValue *pArg) { AppLog("PathFinder:: HandleJavaScriptRequestN"); result r = E_SUCCESS; JsonObject* pJsonObject = static_cast< JsonObject* >(pArg); IJsonValue* pValue = null; JsonString* pJsonStringValue = null; String key(L"data"); r = pJsonObject->GetValue(&key, pValue); pJsonStringValue = static_cast< JsonString* >(pValue); const wchar_t* mapPointString = pJsonStringValue->GetPointer(); AppLog("data: %ls\n", mapPointString); String *tmpString = new String(mapPointString); Float x , y; int idx = 0; tmpString->IndexOf(' ' , 0 , idx); String *tmpString2 = new String ( mapPointString + idx + 1 ); const wchar_t* tmpChar = tmpString->GetPointer(); wchar_t* tmpChar3 = const_cast<wchar_t*>(tmpChar); tmpChar3[idx] = '\0'; const wchar_t* tmpChar2 = tmpString2->GetPointer(); x.Parse(tmpChar3 , this->__latitude ); y.Parse(tmpChar2 , this->__longitude ); }
int norm(int type,Float& y) { // convert y to 1st quadrant angle, and return sign int s=PLUS; Float pi,w,t; if (y.sign()<0) { y.negate(); if (type!=COS) s=-s; } pi=fpi(); w=pi/2; if (fcomp(y,w)<=0) return s; w=2*pi; if (fcomp(y,w)>0) { // reduce mod 2.pi t=y/w; t=trunc(t); t*=w; y-=t; } if (fcomp(y,pi)>0) { y-=pi; if (type!=TAN) s=(-s); } w=pi/2; if (fcomp(y,w)>0) { y=pi-y; if (type!=SIN) s=(-s); } return s; }
Variable* negate(Variable* A) { if(A == NULL) { interpreter.error("Error: Void variable in negation.\n"); return NULL; } TypeEnum a = A->getType(); if(a == STRING || a == BOOL || a == MACRO || a == ARRAY || a == LIST || a == FUNCTION || a == PROCEDURE) { interpreter.error("Error: Negation not defined for type '%s'\n", getTypeString(a).c_str()); return NULL; } if(a == INT) { Int* C = static_cast<Int*>(A); Int* R = new Int; R->setValue(-C->getValue()); return R; } else if(a == FLOAT) { Float* C = static_cast<Float*>(A); Float* R = new Float; R->setValue(-C->getValue()); return R; } return A; }
Number* Rational::atann() { Float* res = new Float; Float* tmp2 = SCAST_FLOAT(res->convert(this)); res->number_ = atan(tmp2->number_); delete tmp2; return res; }
Int Column<Float>::find_one(const Datum &datum) const { // TODO: Choose the best index. Float value = parse_datum(datum); if (!value.is_na() && !indexes_.is_empty()) { return indexes_[0]->find_one(datum); } return scan(value); }
/** @brief Test if dual SVP reduction returns reduced basis. @param A input lattice @param b shortest dual vector @return */ template <class ZT> int test_dsvp_reduce(ZZ_mat<ZT> &A, IntVect &b) { IntMatrix u; int d = A.get_rows(); Float normb; if (dual_length(normb, A, b)) { return 1; } int status = lll_reduction(A, u, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, LLL_DEFAULT); if (status != RED_SUCCESS) { cerr << "LLL reduction failed: " << get_red_status_str(status) << endl; return status; } IntMatrix empty_mat; MatGSO<Integer, Float> gso(A, empty_mat, empty_mat, GSO_INT_GRAM); LLLReduction<Integer, Float> lll_obj(gso, LLL_DEF_DELTA, LLL_DEF_ETA, LLL_DEFAULT); vector<Strategy> strategies; BKZParam dummy(d, strategies); BKZReduction<Float> bkz_obj(gso, lll_obj, dummy); bool clean = true; bkz_obj.svp_reduction_ex(0, d, dummy, clean, true); status = bkz_obj.status; if (status != RED_SUCCESS) { cerr << "Failure: " << get_red_status_str(status) << endl; return status; } Float norm_sol; Integer zero; zero = 0; IntVect e_n(d, zero); e_n[d - 1] = 1; if (dual_length(norm_sol, A, e_n)) { return 1; } Float error; error = 1; error.mul_2si(error, -(int)error.get_prec()); normb += error; if (norm_sol > normb) { cerr << "Last dual vector too long by more than " << error << endl; return 1; } return 0; }
Number *Complex::ang(){ Float *f = new Float(); Float *real1 = SCAST_FLOAT(f->convert(real_)); Float *imag1 = SCAST_FLOAT(f->convert(imag_)); complex<double> a(real1->number_, imag1->number_); Float *result = new Float(arg(a)); delete f, real1, imag1; return result; }
void Column<Float>::unset(Int row_id) { Float value = get(row_id); if (!value.is_na()) { // Update indexes if exist. for (size_t i = 0; i < num_indexes(); ++i) { indexes_[i]->remove(row_id, value); } values_[row_id.raw()] = Float::na(); } }
//----------------------------------------------------------------------------- float DataType::GetFloat() const { HRESULT hr; Float f; hr = f.Cast( *this ); if( FAILED(hr) ) return 0; return f.Get(); }
bool Column<Float>::contains(const Datum &datum) const { // TODO: Choose the best index. Float value = parse_datum(datum); if (!indexes_.is_empty()) { if (value.is_na()) { return table_->num_rows() != indexes_[0]->num_entries(); } return indexes_[0]->contains(datum); } return !scan(value).is_na(); }
Number* Rational::sqt() { if (!num_.sgn_) return new Float(sqrt((double)num_/(double)den_)); else { Complex* resc = new Complex; Float* real = new Float(0.0); Float* imag = new Float(sqrt(fabs(SCAST_FLOAT(real->convert(this))->number_))); resc->real_ = real; resc->imag_ = imag; resc->exact_=false; return resc; } }
Number *Complex::logx(){ Float *f = new Float(); Float *real1 = SCAST_FLOAT(f->convert(real_)); Float *imag1 = SCAST_FLOAT(f->convert(imag_)); complex<double> a(real1->number_, imag1->number_); complex<double> res = log(a); Float *real = new Float(res.real()), *imag = new Float(res.imag()); if (real->number_ == -0) real->number_ = 0; Complex *result = new Complex(real, imag); delete f, real1, imag1, real, imag; return result; }
// Divide val n1 by Float instance n2 and return the quotient, unless // Float instance n2 is equal to zero, then return a zero and send an // error message inline double operator/(const double n1, const Float& n2) { double n = n1; if (n2.getReal() != 0.0f) { n /= n2.getReal(); } else { std::cerr << "Float::operator/(): Divide by zero!" << std::endl; n = 0.0f; } return n; }
Number* Rational::expt(Number* obj){ if(sgn()<0){ Complex* c = new Complex(); c = SCAST_COMPLEX(c->convert(this)); Complex* d = SCAST_COMPLEX(c->convert(obj)); return c->expt(d); }else{ Float* tmpf = new Float(); tmpf = SCAST_FLOAT(tmpf->convert(obj)); return new Float(pow(double(*this), double(*SCAST_RATIONAL(obj)))); } }
//This function demostrate how to use SIX to compute maxmium solution. void solve_linear_program2() { /* Given system has 2 variable, x1, x2 max = 2x1 - x2 s.t. 2x1 - x2 <= 2 x1 - 5x2 <= -4 x1,x2 >= 0 */ Float v; //Init linear inequality. FloatMat leq(2,3); leq.sete(6, 2.0, -1.0, 2.0, 1.0, -5.0, -4.0); //Init target function. FloatMat tgtf(1,3); tgtf.sete(3, 2.0, -1.0, 0.0); //Init variable constrain. FloatMat vc(2,3); vc.sete(6, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0); FloatMat res, eq; SIX<FloatMat,Float> six; //Dump to check. tgtf.dumpf(); vc.dumpf(); leq.dumpf(); /* maximum is 2 solution is: 14/9(1.555556) 10/9(1.111111) 0 */ if (SIX_SUCC == six.maxm(v,res,tgtf,vc,eq,leq)) { printf("\nmaxv is %f\n", v.f()); printf("\nsolution is:\n"); res.dumpf(); } else { printf("\nunbound"); } }
Number *Complex::expt(Number *number2){ Complex *tmp = SCAST_COMPLEX(number2); Float *f = new Float(); Float *real1 =SCAST_FLOAT(f->convert(real_)); Float *imag1 = SCAST_FLOAT(f->convert(imag_)); Float *real2 = SCAST_FLOAT(f->convert(tmp->real_)); Float *imag2 = SCAST_FLOAT(f->convert(tmp->imag_)); complex<double> a(real1->number_, imag1->number_), b(real2->number_, imag2->number_); complex<double> res = pow(a, b); Float *real = new Float(res.real()), *imag = new Float(res.imag()); if (real->number_ == -0) real->number_ = 0; Complex *result = new Complex(real, imag); delete f, real1, imag1, real2, imag2, real, imag; return result; }
dvariable betacf(const dvariable& a, const dvariable& b, const dvariable& x, int MAXIT) { typedef tiny_ad::variable<1, 3> Float; Float a_ (value(a), 0); Float b_ (value(b), 1); Float x_ (value(x), 2); Float ans = betacf<Float>(a_, b_, x_, MAXIT); tiny_vec<double, 3> der = ans.getDeriv(); dvariable hh; value(hh) = ans.value; gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation3ind, &(value(hh)), &(value(a)), der[0] ,&(value(b)), der[1], &(value(x)), der[2]); return hh; }
Complex::Complex(string real, string imag) { type_ = COMPLEX; const char* real2(real.c_str()); const char* imag2(imag.c_str()); Float *f = new Float(); real_ = Rational::from_string(real2); imag_ = Rational::from_string(imag2); if (!real_ || !imag_){ if (!imag_) imag_ = Float::from_string(imag2); else imag_ = f->convert(imag_); if (!real_) real_ = Float::from_string(real2); else real_ = f->convert(real_); if (!real_ || !imag_) throw 0; } }
BOOL Float::add(const Float &b) { // return TRUE if this is affected, FALSE if b has no effect if (b.iszero()) return FALSE; if (iszero()) {*this=b; return TRUE;} Big y=b.m; m.shift(precision-length(m)); // make them precision length y.shift(precision-length(y)); if (e>=b.e) { if (e-b.e>precision) return FALSE; y.shift(b.e-e); m+=y; if (m.iszero()) e=0; else e+=length(m)-precision; } else { if (b.e-e>precision) {*this=b; return TRUE;} m.shift(e-b.e); m+=y; if (m.iszero()) e=0; else e=b.e+length(m)-precision; } m.shift(precision-length(m)); return TRUE; }
Float exp(const Float &a) { // from Brent int i; Float t,x,y,r,one=(Float)1; int q; Big lambda; if (a.iszero()) return one; x=a; q=isqrt(MIRACL*precision,2); lambda=pow((Big)2,q); x/=(Float)lambda; r=0; y=one; for (i=1;i<=q+2;i++) { t=x; t/=i; y*=t; // y*=(x/i); if (!r.add(y)) break; } for (i=0;i<q;i++) { r+=one; r*=r; if (i<q-1) r-=one; } return r; }
Big trunc(const Float& f) { Big b=0; if (f.iszero()) return b; b=shift(f.m,f.e-length(f.m)); return b; }
BOOL Float::sub(const Float &b) { if (b.iszero()) return FALSE; if (iszero()) {*this=-b; return TRUE;} Big y=b.m; m.shift(precision-length(m)); // make them precision length y.shift(precision-length(y)); if (e>=b.e) { if (e-b.e>precision) return FALSE; y.shift(b.e-e); m-=y; if (m.iszero()) e=0; else e+=length(m)-precision; } else { if (b.e-e>precision) {*this=-b; return TRUE;} m.shift(e-b.e); m-=y; if (m.iszero()) e=0; else e=b.e+length(m)-precision; } m.shift(precision-length(m)); return TRUE; }
double todouble(const Float &f) { int i,s; Big x=f.m; double word,d=0.0; mr_small dig; if (f.iszero()) return d; if (f.m>=0) s=PLUS; else {s=MINUS; x.negate();} word=pow(2.0,(double)MIRACL); for (i=0;i<length(x);i++) { dig=x[i]; d+=(double)dig; d/=word; } if (f.e>0) for (i=0;i<f.e;i++) d*=word; if (f.e<0) for (i=0;i<-f.e;i++) d/=word; if (s==MINUS) d=-d; return d; }
Int Column<Float>::scan(Float value) const { if (table_->max_row_id().is_na()) { return Int::na(); } size_t table_size = table_->max_row_id().raw() + 1; size_t valid_size = (values_.size() < table_size) ? values_.size() : table_size; if (value.is_na()) { if (values_.size() < table_size) { return table_->max_row_id(); } bool is_full = table_->is_full(); for (size_t i = 0; i < valid_size; ++i) { if (values_[i].is_na() && (is_full || table_->_test_row(i))) { return Int(i); } } } else { for (size_t i = 0; i < valid_size; ++i) { if (values_[i].match(value)) { return Int(i); } } } return Int::na(); }
template <class ZT> int test_dual_svp(ZZ_mat<ZT> &A, IntVect &b) { IntVect sol_coord; // In the LLL-reduced basis IntVect solution; IntMatrix u; Float normb; if (dual_length(normb, A, b)) { return 1; } int status = lll_reduction(A, u, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, LLL_DEFAULT); if (status != RED_SUCCESS) { cerr << "LLL reduction failed: " << get_red_status_str(status) << endl; return status; } status = shortest_vector(A, sol_coord, SVPM_FAST, SVP_DUAL); if (status != RED_SUCCESS) { cerr << "Failure: " << get_red_status_str(status) << endl; return status; } Float norm_sol; if (dual_length(norm_sol, A, sol_coord)) { return 1; } Float error; error = 1; error.mul_2si(error, -(int)error.get_prec()); normb += error; if (norm_sol > normb) { cerr << "Returned dual vector too long by more than " << error << endl; return 1; } return 0; }
void ComplexTest::constructIdentity() { constexpr Complex a; constexpr Complex b{IdentityInit}; CORRADE_COMPARE(a, Complex(1.0f, 0.0f)); CORRADE_COMPARE(b, Complex(1.0f, 0.0f)); CORRADE_COMPARE(a.length(), 1.0f); CORRADE_COMPARE(b.length(), 1.0f); }
void Float::Assign(Float& v) { if(this->Is_float32()) { if(v.Is_float32()) this->data._float_value = v.data._float_value; else this->data._float_value = v.data._double_value; } else if(this->Is_double64()) { if(v.Is_float32()) this->data._double_value = v.data._float_value; else this->data._double_value = v.data._double_value; } }