RCP<const Basic> mul_expand_two(const RCP<const Basic> &a, const RCP<const Basic> &b) { // Both a and b are assumed to be expanded if (is_a<Add>(*a) && is_a<Add>(*b)) { RCP<const Number> coef = mulnum(rcp_static_cast<const Add>(a)->coef_, rcp_static_cast<const Add>(b)->coef_); umap_basic_num d; // Improves (x+1)**3*(x+2)**3*...(x+350)**3 expansion from 0.97s to 0.93s: d.reserve((rcp_static_cast<const Add>(a))->dict_.size()* (rcp_static_cast<const Add>(b))->dict_.size()); // Expand dicts first: for (auto &p: (rcp_static_cast<const Add>(a))->dict_) { for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { // The main bottleneck here is the mul(p.first, q.first) command RCP<const Basic> term = mul(p.first, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(mulnum(p.second, q.second), rcp_static_cast<const Number>(term))); } else { if (is_a<Mul>(*term) && !(rcp_static_cast<const Mul>(term)->coef_->is_one())) { // Tidy up things like {2x: 3} -> {x: 6} RCP<const Number> coef2 = rcp_static_cast<const Mul>(term)->coef_; // We make a copy of the dict_: map_basic_basic d2 = rcp_static_cast<const Mul>(term)->dict_; term = Mul::from_dict(one, std::move(d2)); Add::dict_add_term(d, mulnum(mulnum(p.second, q.second), coef2), term); } else { Add::dict_add_term(d, mulnum(p.second, q.second), term); } } } Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, p.second), p.first); } // Handle the coefficient of "a": for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(a)->coef_, q.second), q.first); } return Add::from_dict(coef, std::move(d)); } else if (is_a<Add>(*a)) { return mul_expand_two(b, a); } else if (is_a<Add>(*b)) { RCP<const Number> a_coef; RCP<const Basic> a_term; Add::as_coef_term(a, outArg(a_coef), outArg(a_term)); RCP<const Number> coef = zero; umap_basic_num d; d.reserve((rcp_static_cast<const Add>(b))->dict_.size()); for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { RCP<const Basic> term = mul(a_term, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(mulnum(q.second, a_coef), rcp_static_cast<const Number>(term))); } else { if (is_a<Mul>(*term) && !(rcp_static_cast<const Mul>(term)->coef_->is_one())) { // Tidy up things like {2x: 3} -> {x: 6} RCP<const Number> coef2 = rcp_static_cast<const Mul>(term)->coef_; // We make a copy of the dict_: map_basic_basic d2 = rcp_static_cast<const Mul>(term)->dict_; term = Mul::from_dict(one, std::move(d2)); Add::dict_add_term(d, mulnum(mulnum(q.second, a_coef), coef2), term); } else { Add::dict_add_term(d, mulnum(a_coef, q.second), term); } } } if (eq(*a_term, *one)) { iaddnum(outArg(coef), mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef)); } else { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef), a_term); } return Add::from_dict(coef, std::move(d)); } return mul(a, b); }
void determinant(ZZ_p& d, const mat_ZZ_p& M_in) { long k, n; long i, j; long pos; ZZ t1, t2; ZZ *x, *y; const ZZ& p = ZZ_p::modulus(); n = M_in.NumRows(); if (M_in.NumCols() != n) LogicError("determinant: nonsquare matrix"); if (n == 0) { set(d); return; } vec_ZZVec M; sqr(t1, p); mul(t1, t1, n); M.SetLength(n); for (i = 0; i < n; i++) { M[i].SetSize(n, t1.size()); for (j = 0; j < n; j++) M[i][j] = rep(M_in[i][j]); } ZZ det; set(det); for (k = 0; k < n; k++) { pos = -1; for (i = k; i < n; i++) { rem(t1, M[i][k], p); M[i][k] = t1; if (pos == -1 && !IsZero(t1)) pos = i; } if (pos != -1) { if (k != pos) { swap(M[pos], M[k]); NegateMod(det, det, p); } MulMod(det, det, M[k][k], p); // make M[k, k] == -1 mod p, and make row k reduced InvMod(t1, M[k][k], p); NegateMod(t1, t1, p); for (j = k+1; j < n; j++) { rem(t2, M[k][j], p); MulMod(M[k][j], t2, t1, p); } for (i = k+1; i < n; i++) { // M[i] = M[i] + M[k]*M[i,k] t1 = M[i][k]; // this is already reduced x = M[i].elts() + (k+1); y = M[k].elts() + (k+1); for (j = k+1; j < n; j++, x++, y++) { // *x = *x + (*y)*t1 mul(t2, *y, t1); add(*x, *x, t2); } } } else { clear(d); return; } } conv(d, det); }
void invert(int32_t *out, int32_t const *in) { int32_t t0[10], t1[10], t2[10], t3[10], i; sqr(t0,in); sqr(t1,t0); sqr(t1,t1); mul(t1,in,t1); mul(t0,t0,t1); sqr(t2,t0); mul(t1,t1,t2); sqr(t2,t1); for (i = 1;i < 5;++i) sqr(t2,t2); mul(t1,t2,t1); sqr(t2,t1); for (i = 1;i < 10;++i) sqr(t2,t2); mul(t2,t2,t1); sqr(t3,t2); for (i = 1;i < 20;++i) sqr(t3,t3); mul(t2,t3,t2); sqr(t2,t2); for (i = 1;i < 10;++i) sqr(t2,t2); mul(t1,t2,t1); sqr(t2,t1); for (i = 1;i < 50;++i) sqr(t2,t2); mul(t2,t2,t1); sqr(t3,t2); for (i = 1;i < 100;++i) sqr(t3,t3); mul(t2,t3,t2); sqr(t2,t2); for (i = 1;i < 50;++i) sqr(t2,t2); mul(t1,t2,t1); sqr(t1,t1); for (i = 1;i < 5;++i) sqr(t1,t1); mul(out,t1,t0); }
RCP<const Basic> div(const RCP<const Basic> &a, const RCP<const Basic> &b) { return mul(a, pow(b, minus_one)); }
void *render_pixel(void *thread_info) { /* for every pixel */ struct information* info = (struct information*) thread_info; for( int px=info->start; px<info->end; ++px ) { const double x = pixel_dx * ((double)( px-(width/2) )); for( int py=0; py<height; ++py ) { const double y = pixel_dy * ((double)( py-(height/2) )); Vec3 pixel_color; set( pixel_color, 0, 0, 0 ); for( int xs=-halfSamples; xs<=halfSamples; ++xs ) { for( int ys=-halfSamples; ys<=halfSamples; ++ys ) { double subx = x + ((double)xs)*subsample_dx; double suby = y + ((double)ys)*subsample_dy; /* construct the ray coming out of the camera, through * the screen at (subx,suby) */ ray_t pixel_ray; copy( pixel_ray.org, camera_pos ); Vec3 pixel_target; set( pixel_target, subx, suby, z ); sub( pixel_ray.dir, pixel_target, camera_pos ); norm( pixel_ray.dir, pixel_ray.dir ); Vec3 sample_color; copy( sample_color, bg_color ); /* trace the ray from the camera that * passes through this pixel */ trace( &scene, sample_color, &pixel_ray, 0 ); /* sum color for subpixel AA */ add( pixel_color, pixel_color, sample_color ); } } /* at this point, have accumulated (2*halfSamples)^2 samples, * so need to average out the final pixel color */ if( halfSamples ) { mul( pixel_color, pixel_color, (1.0/( 4.0 * halfSamples * halfSamples ) ) ); } /* done, final floating point color values are in pixel_color */ float scaled_color[3]; scaled_color[0] = gamma( pixel_color[0] ) * max_color; scaled_color[1] = gamma( pixel_color[1] ) * max_color; scaled_color[2] = gamma( pixel_color[2] ) * max_color; /* enforce caps, replace with real gamma */ for( int i=0; i<3; i++) s_color[px][py][i] = max( min(scaled_color[i], 255), 0); } } pthread_exit(NULL); }
/* * L3_mdct_sub: * ------------ */ void L3_mdct_sub(long sb_sample[2][3][18][SBLIMIT], long mdct_freq[2][2][samp_per_frame2], config_t *config) { /* note. we wish to access the array 'mdct_freq[2][2][576]' as * [2][2][32][18]. (32*18=576), */ long (*mdct_enc)[18]; int ch,gr,band,j,k; long mdct_in[36]; long bu,bd,*m; //for(gr=0; gr<2; gr++) for(gr=0; gr<config->mpeg.mode_gr; gr++) for(ch=config->wave.channels; ch--; ) { /* set up pointer to the part of mdct_freq we're using */ mdct_enc = (long (*)[18]) mdct_freq[gr][ch]; /* Compensate for inversion in the analysis filter * (every odd index of band AND k) */ for(band=1; band<=31; band+=2 ) for(k=1; k<=17; k+=2 ) sb_sample[ch][gr+1][k][band] *= -1; /* Perform imdct of 18 previous subband samples + 18 current subband samples */ for(band=32; band--; ) { for(k=18; k--; ) { mdct_in[k] = sb_sample[ch][ gr ][k][band]; mdct_in[k+18] = sb_sample[ch][gr+1][k][band]; } /* Calculation of the MDCT * In the case of long blocks ( block_type 0,1,3 ) there are * 36 coefficients in the time domain and 18 in the frequency * domain. */ for(k=18; k--; ) { m = &mdct_enc[band][k]; for(j=36, *m=0; j--; ) *m += mul(mdct_in[j],cos_l[k][j]); } } /* Perform aliasing reduction butterfly */ for(band=31; band--; ) for(k=8; k--; ) { /* must left justify result of multiplication here because the centre * two values in each block are not touched. */ bu = muls(mdct_enc[band][17-k],cs[k]) + muls(mdct_enc[band+1][k],ca[k]); bd = muls(mdct_enc[band+1][k],cs[k]) - muls(mdct_enc[band][17-k],ca[k]); mdct_enc[band][17-k] = bu; mdct_enc[band+1][k] = bd; } } /* Save latest granule's subband samples to be used in the next mdct call */ for(ch=config->wave.channels ;ch--; ) for(j=18; j--; ) for(band=32; band--; ) sb_sample[ch][0][j][band] = sb_sample[ch][config->mpeg.mode_gr][j][band]; }
void LVIMU::Timestep(double simt) { double deltaTime, pulses; if (!Operate) { if (IsPowered()) TurnOn(); else return; } else if (!IsPowered()) { TurnOff(); return; } // // If we get here, we're powered up. // if (!TurnedOn) { return; } // fill OrbiterData VESSELSTATUS vs; OurVessel->GetStatus(vs); Orbiter.Attitude.X = vs.arot.x; Orbiter.Attitude.Y = vs.arot.y; Orbiter.Attitude.Z = vs.arot.z; // Vessel to Orbiter global transformation MATRIX3 tinv = getRotationMatrixZ(-vs.arot.z); tinv = mul(getRotationMatrixY(-vs.arot.y), tinv); tinv = mul(getRotationMatrixX(-vs.arot.x), tinv); if (!Initialized) { SetOrbiterAttitudeReference(); // Get current weight vector in vessel coordinates VECTOR3 w; OurVessel->GetWeightVector(w); // Transform to Orbiter global and calculate weight acceleration w = mul(tinv, w) / OurVessel->GetMass(); LastWeightAcceleration = w; OurVessel->GetGlobalVel(LastGlobalVel); LastTime = simt; Initialized = true; } else { deltaTime = (simt - LastTime); // Calculate accelerations VECTOR3 w, vel; OurVessel->GetWeightVector(w); // Transform to Orbiter global and calculate accelerations w = mul(tinv, w) / OurVessel->GetMass(); OurVessel->GetGlobalVel(vel); VECTOR3 dvel = (vel - LastGlobalVel) / deltaTime; // Measurements with the 2006-P1 version showed that the average of the weight // vector of this and the last step match the force vector while in free fall // The force vector matches the global velocity change of the last timestep exactly, // but isn't used because GetForceVector isn't working while docked VECTOR3 dw1 = w - dvel; VECTOR3 dw2 = LastWeightAcceleration - dvel; VECTOR3 accel = -(dw1 + dw2) / 2.0; LastWeightAcceleration = w; LastGlobalVel = vel; // orbiter earth rotation // imuState->Orbiter.Y = imuState->Orbiter.Y + (deltaTime * TwoPI / 86164.09); // sprintf(oapiDebugString(), "accel x %.10f y %.10f z %.10f DT %f", accel.x, accel.y, accel.z, deltaTime); // Process channel bits if (ZeroIMUCDUFlag) { ZeroIMUCDUs(); } else if (CoarseAlignEnableFlag) { SetOrbiterAttitudeReference(); } else if (Caged) { SetOrbiterAttitudeReference(); } else { // Gimbals MATRIX3 t = Orbiter.AttitudeReference; t = mul(getRotationMatrixX(Orbiter.Attitude.X), t); t = mul(getRotationMatrixY(Orbiter.Attitude.Y), t); t = mul(getRotationMatrixZ(Orbiter.Attitude.Z), t); t = mul(getOrbiterLocalToNavigationBaseTransformation(), t); // calculate the new gimbal angles VECTOR3 newAngles = getRotationAnglesXZY(t); // drive gimbals to new angles // CAUTION: gimbal angles are left-handed DriveGimbalX(-newAngles.x - Gimbal.X); DriveGimbalY(-newAngles.y - Gimbal.Y); DriveGimbalZ(-newAngles.z - Gimbal.Z); // PIPAs accel = tmul(Orbiter.AttitudeReference, accel); // sprintf(oapiDebugString(), "accel x %.10f y %.10f z %.10f DT %f", accel.x, accel.y, accel.z, deltaTime); // pulse PIPAs //pulses = RemainingPIPA.X + (accel.x * deltaTime / 0.0585); //PulsePIPA(LVRegPIPAX, (int) pulses); //RemainingPIPA.X = pulses - (int) pulses; //pulses = RemainingPIPA.Y + (accel.y * deltaTime / 0.0585); //PulsePIPA(LVRegPIPAY, (int) pulses); //RemainingPIPA.Y = pulses - (int) pulses; //pulses = RemainingPIPA.Z + (accel.z * deltaTime / 0.0585); //PulsePIPA(LVRegPIPAZ, (int) pulses); //RemainingPIPA.Z = pulses - (int) pulses; pulses = (accel.x * deltaTime); PulsePIPA(LVRegPIPAX, pulses); pulses = (accel.y * deltaTime); PulsePIPA(LVRegPIPAY, pulses); pulses = (accel.z * deltaTime); PulsePIPA(LVRegPIPAZ, pulses); } LastTime = simt; } }
void kernel(mat_ZZ_p& X, const mat_ZZ_p& A) { long m = A.NumRows(); long n = A.NumCols(); mat_ZZ_p M; long r; transpose(M, A); r = gauss(M); X.SetDims(m-r, m); long i, j, k, s; ZZ t1, t2; ZZ_p T3; vec_long D; D.SetLength(m); for (j = 0; j < m; j++) D[j] = -1; vec_ZZ_p inverses; inverses.SetLength(m); j = -1; for (i = 0; i < r; i++) { do { j++; } while (IsZero(M[i][j])); D[j] = i; inv(inverses[j], M[i][j]); } for (k = 0; k < m-r; k++) { vec_ZZ_p& v = X[k]; long pos = 0; for (j = m-1; j >= 0; j--) { if (D[j] == -1) { if (pos == k) set(v[j]); else clear(v[j]); pos++; } else { i = D[j]; clear(t1); for (s = j+1; s < m; s++) { mul(t2, rep(v[s]), rep(M[i][s])); add(t1, t1, t2); } conv(T3, t1); mul(T3, T3, inverses[j]); negate(v[j], T3); } } } }
inline vector::TVector3 <T> operator * (const vector::TVector3<T> &a, const matrix::TMatrix3x3 <T> &b) { return mul(a, b); }
node div(node a,node b){ node ret; node temp; temp.den=b.num; temp.num=b.den; return ret=mul(a,temp); }
inline TMatrix3x1 <T> operator * (const TMatrix3x3 <T> &a, const TMatrix3x1 <T> &b) { return mul(a, b); }
void InvMixColumn() { /* Mix the four bytes of every column in a linear way * This is the opposite operation of Mixcolumn */ word8 AES_MEMORY b[4][MAXBC]; b[0][0] = mul ( 0xe, block[0][0] ) ^ mul ( 0xb, block[ ( 0+1 ) %4][0] ) ^ mul ( 0xd, block[ ( 0+2 ) %4][0] ) ^ mul ( 0x9, block[ ( 0+3 ) %4][0] ); b[1][0] = mul ( 0xe, block[1][0] ) ^ mul ( 0xb, block[ ( 1+1 ) %4][0] ) ^ mul ( 0xd, block[ ( 1+2 ) %4][0] ) ^ mul ( 0x9, block[ ( 1+3 ) %4][0] ); b[2][0] = mul ( 0xe, block[2][0] ) ^ mul ( 0xb, block[ ( 2+1 ) %4][0] ) ^ mul ( 0xd, block[ ( 2+2 ) %4][0] ) ^ mul ( 0x9, block[ ( 2+3 ) %4][0] ); b[3][0] = mul ( 0xe, block[3][0] ) ^ mul ( 0xb, block[ ( 3+1 ) %4][0] ) ^ mul ( 0xd, block[ ( 3+2 ) %4][0] ) ^ mul ( 0x9, block[ ( 3+3 ) %4][0] ); b[0][1] = mul ( 0xe, block[0][1] ) ^ mul ( 0xb, block[ ( 0+1 ) %4][1] ) ^ mul ( 0xd, block[ ( 0+2 ) %4][1] ) ^ mul ( 0x9, block[ ( 0+3 ) %4][1] ); b[1][1] = mul ( 0xe, block[1][1] ) ^ mul ( 0xb, block[ ( 1+1 ) %4][1] ) ^ mul ( 0xd, block[ ( 1+2 ) %4][1] ) ^ mul ( 0x9, block[ ( 1+3 ) %4][1] ); b[2][1] = mul ( 0xe, block[2][1] ) ^ mul ( 0xb, block[ ( 2+1 ) %4][1] ) ^ mul ( 0xd, block[ ( 2+2 ) %4][1] ) ^ mul ( 0x9, block[ ( 2+3 ) %4][1] ); b[3][1] = mul ( 0xe, block[3][1] ) ^ mul ( 0xb, block[ ( 3+1 ) %4][1] ) ^ mul ( 0xd, block[ ( 3+2 ) %4][1] ) ^ mul ( 0x9, block[ ( 3+3 ) %4][1] ); b[0][2] = mul ( 0xe, block[0][2] ) ^ mul ( 0xb, block[ ( 0+1 ) %4][2] ) ^ mul ( 0xd, block[ ( 0+2 ) %4][2] ) ^ mul ( 0x9, block[ ( 0+3 ) %4][2] ); b[1][2] = mul ( 0xe, block[1][2] ) ^ mul ( 0xb, block[ ( 1+1 ) %4][2] ) ^ mul ( 0xd, block[ ( 1+2 ) %4][2] ) ^ mul ( 0x9, block[ ( 1+3 ) %4][2] ); b[2][2] = mul ( 0xe, block[2][2] ) ^ mul ( 0xb, block[ ( 2+1 ) %4][2] ) ^ mul ( 0xd, block[ ( 2+2 ) %4][2] ) ^ mul ( 0x9, block[ ( 2+3 ) %4][2] ); b[3][2] = mul ( 0xe, block[3][2] ) ^ mul ( 0xb, block[ ( 3+1 ) %4][2] ) ^ mul ( 0xd, block[ ( 3+2 ) %4][2] ) ^ mul ( 0x9, block[ ( 3+3 ) %4][2] ); b[0][3] = mul ( 0xe, block[0][3] ) ^ mul ( 0xb, block[ ( 0+1 ) %4][3] ) ^ mul ( 0xd, block[ ( 0+2 ) %4][3] ) ^ mul ( 0x9, block[ ( 0+3 ) %4][3] ); b[1][3] = mul ( 0xe, block[1][3] ) ^ mul ( 0xb, block[ ( 1+1 ) %4][3] ) ^ mul ( 0xd, block[ ( 1+2 ) %4][3] ) ^ mul ( 0x9, block[ ( 1+3 ) %4][3] ); b[2][3] = mul ( 0xe, block[2][3] ) ^ mul ( 0xb, block[ ( 2+1 ) %4][3] ) ^ mul ( 0xd, block[ ( 2+2 ) %4][3] ) ^ mul ( 0x9, block[ ( 2+3 ) %4][3] ); b[3][3] = mul ( 0xe, block[3][3] ) ^ mul ( 0xb, block[ ( 3+1 ) %4][3] ) ^ mul ( 0xd, block[ ( 3+2 ) %4][3] ) ^ mul ( 0x9, block[ ( 3+3 ) %4][3] ); block[0][0] = b[0][0]; block[0][1] = b[0][1]; block[0][2] = b[0][2]; block[0][3] = b[0][3]; block[1][0] = b[1][0]; block[1][1] = b[1][1]; block[1][2] = b[1][2]; block[1][3] = b[1][3]; block[2][0] = b[2][0]; block[2][1] = b[2][1]; block[2][2] = b[2][2]; block[2][3] = b[2][3]; block[3][0] = b[3][0]; block[3][1] = b[3][1]; block[3][2] = b[3][2]; block[3][3] = b[3][3]; }
void MixColumn() { /* Mix the four bytes of every column in a linear way */ word8 AES_MEMORY b[4][MAXBC]; /* TRIALOG decomposition of operation */ b[0][0] = mul ( 2, block[0][0] ) ^ mul ( 3, block[ ( 0 + 1 ) % 4][0] ) ^ block[ ( 0 + 2 ) % 4][0] ^ block[ ( 0 + 3 ) % 4][0]; b[0][1] = mul ( 2, block[0][1] ) ^ mul ( 3, block[ ( 0 + 1 ) % 4][1] ) ^ block[ ( 0 + 2 ) % 4][1] ^ block[ ( 0 + 3 ) % 4][1]; b[0][2] = mul ( 2, block[0][2] ) ^ mul ( 3, block[ ( 0 + 1 ) % 4][2] ) ^ block[ ( 0 + 2 ) % 4][2] ^ block[ ( 0 + 3 ) % 4][2]; b[0][3] = mul ( 2, block[0][3] ) ^ mul ( 3, block[ ( 0 + 1 ) % 4][3] ) ^ block[ ( 0 + 2 ) % 4][3] ^ block[ ( 0 + 3 ) % 4][3]; b[1][0] = mul ( 2, block[1][0] ) ^ mul ( 3, block[ ( 1 + 1 ) % 4][0] ) ^ block[ ( 1 + 2 ) % 4][0] ^ block[ ( 1 + 3 ) % 4][0]; b[1][1] = mul ( 2, block[1][1] ) ^ mul ( 3, block[ ( 1 + 1 ) % 4][1] ) ^ block[ ( 1 + 2 ) % 4][1] ^ block[ ( 1 + 3 ) % 4][1]; b[1][2] = mul ( 2, block[1][2] ) ^ mul ( 3, block[ ( 1 + 1 ) % 4][2] ) ^ block[ ( 1 + 2 ) % 4][2] ^ block[ ( 1 + 3 ) % 4][2]; b[1][3] = mul ( 2, block[1][3] ) ^ mul ( 3, block[ ( 1 + 1 ) % 4][3] ) ^ block[ ( 1 + 2 ) % 4][3] ^ block[ ( 1 + 3 ) % 4][3]; b[2][0] = mul ( 2, block[2][0] ) ^ mul ( 3, block[ ( 2 + 1 ) % 4][0] ) ^ block[ ( 2 + 2 ) % 4][0] ^ block[ ( 2 + 3 ) % 4][0]; b[2][1] = mul ( 2, block[2][1] ) ^ mul ( 3, block[ ( 2 + 1 ) % 4][1] ) ^ block[ ( 2 + 2 ) % 4][1] ^ block[ ( 2 + 3 ) % 4][1]; b[2][2] = mul ( 2, block[2][2] ) ^ mul ( 3, block[ ( 2 + 1 ) % 4][2] ) ^ block[ ( 2 + 2 ) % 4][2] ^ block[ ( 2 + 3 ) % 4][2]; b[2][3] = mul ( 2, block[2][3] ) ^ mul ( 3, block[ ( 2 + 1 ) % 4][3] ) ^ block[ ( 2 + 2 ) % 4][3] ^ block[ ( 2 + 3 ) % 4][3]; b[3][0] = mul ( 2, block[3][0] ) ^ mul ( 3, block[ ( 3 + 1 ) % 4][0] ) ^ block[ ( 3 + 2 ) % 4][0] ^ block[ ( 3 + 3 ) % 4][0]; b[3][1] = mul ( 2, block[3][1] ) ^ mul ( 3, block[ ( 3 + 1 ) % 4][1] ) ^ block[ ( 3 + 2 ) % 4][1] ^ block[ ( 3 + 3 ) % 4][1]; b[3][2] = mul ( 2, block[3][2] ) ^ mul ( 3, block[ ( 3 + 1 ) % 4][2] ) ^ block[ ( 3 + 2 ) % 4][2] ^ block[ ( 3 + 3 ) % 4][2]; b[3][3] = mul ( 2, block[3][3] ) ^ mul ( 3, block[ ( 3 + 1 ) % 4][3] ) ^ block[ ( 3 + 2 ) % 4][3] ^ block[ ( 3 + 3 ) % 4][3]; block[0][0] = b[0][0]; block[0][1] = b[0][1]; block[0][2] = b[0][2]; block[0][3] = b[0][3]; block[1][0] = b[1][0]; block[1][1] = b[1][1]; block[1][2] = b[1][2]; block[1][3] = b[1][3]; block[2][0] = b[2][0]; block[2][1] = b[2][1]; block[2][2] = b[2][2]; block[2][3] = b[2][3]; block[3][0] = b[3][0]; block[3][1] = b[3][1]; block[3][2] = b[3][2]; block[3][3] = b[3][3]; }
G2 PFC::mult(const G2& w,const Big& k) { G2 z; int i; if (w.mtable!=NULL) { // we have precomputed values Big e=k; if (k<0) e=-e; int i,j,t=w.mtbits; //MR_ROUNDUP(2*S,WINDOW_SIZE); j=recode(e,t,WINDOW_SIZE,t-1); z.g=w.mtable[j]; for (i=t-2;i>=0;i--) { j=recode(e,t,WINDOW_SIZE,i); z.g+=z.g; if (j>0) z.g+=w.mtable[j]; } if (k<0) z.g=-z.g; } else { ECn3 Q[6]; Big u[6]; BOOL small=TRUE; galscott(k,*ord,WB,BB,u); Q[0]=w.g; for (i=1;i<6;i++) { if (u[i]!=0) { small=FALSE; break; } } if (small) { if (u[0]<0) { u[0]=-u[0]; Q[0]=-Q[0]; } z.g=Q[0]; z.g*=u[0]; return z; } for (i=1;i<6;i++) Q[i]=psi(Q[i-1],*frob,1); // deal with -ve multipliers for (i=0;i<6;i++) { if (u[i]<0) {u[i]=-u[i];Q[i]=-Q[i];} } // simple multi-addition z.g= mul(6,Q,u); } return z; }
/* execute instructions on this CPU until icount expires */ static CPU_EXECUTE( m6809 ) /* NS 970908 */ { m68_state_t *m68_state = get_safe_token(device); m68_state->icount = cycles - m68_state->extra_cycles; m68_state->extra_cycles = 0; check_irq_lines(m68_state); if (m68_state->int_state & (M6809_CWAI | M6809_SYNC)) { debugger_instruction_hook(device, PCD); m68_state->icount = 0; } else { do { pPPC = pPC; debugger_instruction_hook(device, PCD); m68_state->ireg = ROP(PCD); PC++; #if BIG_SWITCH switch( m68_state->ireg ) { case 0x00: neg_di(m68_state); break; case 0x01: neg_di(m68_state); break; /* undocumented */ case 0x02: IIError(m68_state); break; case 0x03: com_di(m68_state); break; case 0x04: lsr_di(m68_state); break; case 0x05: IIError(m68_state); break; case 0x06: ror_di(m68_state); break; case 0x07: asr_di(m68_state); break; case 0x08: asl_di(m68_state); break; case 0x09: rol_di(m68_state); break; case 0x0a: dec_di(m68_state); break; case 0x0b: IIError(m68_state); break; case 0x0c: inc_di(m68_state); break; case 0x0d: tst_di(m68_state); break; case 0x0e: jmp_di(m68_state); break; case 0x0f: clr_di(m68_state); break; case 0x10: pref10(m68_state); break; case 0x11: pref11(m68_state); break; case 0x12: nop(m68_state); break; case 0x13: sync(m68_state); break; case 0x14: IIError(m68_state); break; case 0x15: IIError(m68_state); break; case 0x16: lbra(m68_state); break; case 0x17: lbsr(m68_state); break; case 0x18: IIError(m68_state); break; case 0x19: daa(m68_state); break; case 0x1a: orcc(m68_state); break; case 0x1b: IIError(m68_state); break; case 0x1c: andcc(m68_state); break; case 0x1d: sex(m68_state); break; case 0x1e: exg(m68_state); break; case 0x1f: tfr(m68_state); break; case 0x20: bra(m68_state); break; case 0x21: brn(m68_state); break; case 0x22: bhi(m68_state); break; case 0x23: bls(m68_state); break; case 0x24: bcc(m68_state); break; case 0x25: bcs(m68_state); break; case 0x26: bne(m68_state); break; case 0x27: beq(m68_state); break; case 0x28: bvc(m68_state); break; case 0x29: bvs(m68_state); break; case 0x2a: bpl(m68_state); break; case 0x2b: bmi(m68_state); break; case 0x2c: bge(m68_state); break; case 0x2d: blt(m68_state); break; case 0x2e: bgt(m68_state); break; case 0x2f: ble(m68_state); break; case 0x30: leax(m68_state); break; case 0x31: leay(m68_state); break; case 0x32: leas(m68_state); break; case 0x33: leau(m68_state); break; case 0x34: pshs(m68_state); break; case 0x35: puls(m68_state); break; case 0x36: pshu(m68_state); break; case 0x37: pulu(m68_state); break; case 0x38: IIError(m68_state); break; case 0x39: rts(m68_state); break; case 0x3a: abx(m68_state); break; case 0x3b: rti(m68_state); break; case 0x3c: cwai(m68_state); break; case 0x3d: mul(m68_state); break; case 0x3e: IIError(m68_state); break; case 0x3f: swi(m68_state); break; case 0x40: nega(m68_state); break; case 0x41: IIError(m68_state); break; case 0x42: IIError(m68_state); break; case 0x43: coma(m68_state); break; case 0x44: lsra(m68_state); break; case 0x45: IIError(m68_state); break; case 0x46: rora(m68_state); break; case 0x47: asra(m68_state); break; case 0x48: asla(m68_state); break; case 0x49: rola(m68_state); break; case 0x4a: deca(m68_state); break; case 0x4b: IIError(m68_state); break; case 0x4c: inca(m68_state); break; case 0x4d: tsta(m68_state); break; case 0x4e: IIError(m68_state); break; case 0x4f: clra(m68_state); break; case 0x50: negb(m68_state); break; case 0x51: IIError(m68_state); break; case 0x52: IIError(m68_state); break; case 0x53: comb(m68_state); break; case 0x54: lsrb(m68_state); break; case 0x55: IIError(m68_state); break; case 0x56: rorb(m68_state); break; case 0x57: asrb(m68_state); break; case 0x58: aslb(m68_state); break; case 0x59: rolb(m68_state); break; case 0x5a: decb(m68_state); break; case 0x5b: IIError(m68_state); break; case 0x5c: incb(m68_state); break; case 0x5d: tstb(m68_state); break; case 0x5e: IIError(m68_state); break; case 0x5f: clrb(m68_state); break; case 0x60: neg_ix(m68_state); break; case 0x61: IIError(m68_state); break; case 0x62: IIError(m68_state); break; case 0x63: com_ix(m68_state); break; case 0x64: lsr_ix(m68_state); break; case 0x65: IIError(m68_state); break; case 0x66: ror_ix(m68_state); break; case 0x67: asr_ix(m68_state); break; case 0x68: asl_ix(m68_state); break; case 0x69: rol_ix(m68_state); break; case 0x6a: dec_ix(m68_state); break; case 0x6b: IIError(m68_state); break; case 0x6c: inc_ix(m68_state); break; case 0x6d: tst_ix(m68_state); break; case 0x6e: jmp_ix(m68_state); break; case 0x6f: clr_ix(m68_state); break; case 0x70: neg_ex(m68_state); break; case 0x71: IIError(m68_state); break; case 0x72: IIError(m68_state); break; case 0x73: com_ex(m68_state); break; case 0x74: lsr_ex(m68_state); break; case 0x75: IIError(m68_state); break; case 0x76: ror_ex(m68_state); break; case 0x77: asr_ex(m68_state); break; case 0x78: asl_ex(m68_state); break; case 0x79: rol_ex(m68_state); break; case 0x7a: dec_ex(m68_state); break; case 0x7b: IIError(m68_state); break; case 0x7c: inc_ex(m68_state); break; case 0x7d: tst_ex(m68_state); break; case 0x7e: jmp_ex(m68_state); break; case 0x7f: clr_ex(m68_state); break; case 0x80: suba_im(m68_state); break; case 0x81: cmpa_im(m68_state); break; case 0x82: sbca_im(m68_state); break; case 0x83: subd_im(m68_state); break; case 0x84: anda_im(m68_state); break; case 0x85: bita_im(m68_state); break; case 0x86: lda_im(m68_state); break; case 0x87: sta_im(m68_state); break; case 0x88: eora_im(m68_state); break; case 0x89: adca_im(m68_state); break; case 0x8a: ora_im(m68_state); break; case 0x8b: adda_im(m68_state); break; case 0x8c: cmpx_im(m68_state); break; case 0x8d: bsr(m68_state); break; case 0x8e: ldx_im(m68_state); break; case 0x8f: stx_im(m68_state); break; case 0x90: suba_di(m68_state); break; case 0x91: cmpa_di(m68_state); break; case 0x92: sbca_di(m68_state); break; case 0x93: subd_di(m68_state); break; case 0x94: anda_di(m68_state); break; case 0x95: bita_di(m68_state); break; case 0x96: lda_di(m68_state); break; case 0x97: sta_di(m68_state); break; case 0x98: eora_di(m68_state); break; case 0x99: adca_di(m68_state); break; case 0x9a: ora_di(m68_state); break; case 0x9b: adda_di(m68_state); break; case 0x9c: cmpx_di(m68_state); break; case 0x9d: jsr_di(m68_state); break; case 0x9e: ldx_di(m68_state); break; case 0x9f: stx_di(m68_state); break; case 0xa0: suba_ix(m68_state); break; case 0xa1: cmpa_ix(m68_state); break; case 0xa2: sbca_ix(m68_state); break; case 0xa3: subd_ix(m68_state); break; case 0xa4: anda_ix(m68_state); break; case 0xa5: bita_ix(m68_state); break; case 0xa6: lda_ix(m68_state); break; case 0xa7: sta_ix(m68_state); break; case 0xa8: eora_ix(m68_state); break; case 0xa9: adca_ix(m68_state); break; case 0xaa: ora_ix(m68_state); break; case 0xab: adda_ix(m68_state); break; case 0xac: cmpx_ix(m68_state); break; case 0xad: jsr_ix(m68_state); break; case 0xae: ldx_ix(m68_state); break; case 0xaf: stx_ix(m68_state); break; case 0xb0: suba_ex(m68_state); break; case 0xb1: cmpa_ex(m68_state); break; case 0xb2: sbca_ex(m68_state); break; case 0xb3: subd_ex(m68_state); break; case 0xb4: anda_ex(m68_state); break; case 0xb5: bita_ex(m68_state); break; case 0xb6: lda_ex(m68_state); break; case 0xb7: sta_ex(m68_state); break; case 0xb8: eora_ex(m68_state); break; case 0xb9: adca_ex(m68_state); break; case 0xba: ora_ex(m68_state); break; case 0xbb: adda_ex(m68_state); break; case 0xbc: cmpx_ex(m68_state); break; case 0xbd: jsr_ex(m68_state); break; case 0xbe: ldx_ex(m68_state); break; case 0xbf: stx_ex(m68_state); break; case 0xc0: subb_im(m68_state); break; case 0xc1: cmpb_im(m68_state); break; case 0xc2: sbcb_im(m68_state); break; case 0xc3: addd_im(m68_state); break; case 0xc4: andb_im(m68_state); break; case 0xc5: bitb_im(m68_state); break; case 0xc6: ldb_im(m68_state); break; case 0xc7: stb_im(m68_state); break; case 0xc8: eorb_im(m68_state); break; case 0xc9: adcb_im(m68_state); break; case 0xca: orb_im(m68_state); break; case 0xcb: addb_im(m68_state); break; case 0xcc: ldd_im(m68_state); break; case 0xcd: std_im(m68_state); break; case 0xce: ldu_im(m68_state); break; case 0xcf: stu_im(m68_state); break; case 0xd0: subb_di(m68_state); break; case 0xd1: cmpb_di(m68_state); break; case 0xd2: sbcb_di(m68_state); break; case 0xd3: addd_di(m68_state); break; case 0xd4: andb_di(m68_state); break; case 0xd5: bitb_di(m68_state); break; case 0xd6: ldb_di(m68_state); break; case 0xd7: stb_di(m68_state); break; case 0xd8: eorb_di(m68_state); break; case 0xd9: adcb_di(m68_state); break; case 0xda: orb_di(m68_state); break; case 0xdb: addb_di(m68_state); break; case 0xdc: ldd_di(m68_state); break; case 0xdd: std_di(m68_state); break; case 0xde: ldu_di(m68_state); break; case 0xdf: stu_di(m68_state); break; case 0xe0: subb_ix(m68_state); break; case 0xe1: cmpb_ix(m68_state); break; case 0xe2: sbcb_ix(m68_state); break; case 0xe3: addd_ix(m68_state); break; case 0xe4: andb_ix(m68_state); break; case 0xe5: bitb_ix(m68_state); break; case 0xe6: ldb_ix(m68_state); break; case 0xe7: stb_ix(m68_state); break; case 0xe8: eorb_ix(m68_state); break; case 0xe9: adcb_ix(m68_state); break; case 0xea: orb_ix(m68_state); break; case 0xeb: addb_ix(m68_state); break; case 0xec: ldd_ix(m68_state); break; case 0xed: std_ix(m68_state); break; case 0xee: ldu_ix(m68_state); break; case 0xef: stu_ix(m68_state); break; case 0xf0: subb_ex(m68_state); break; case 0xf1: cmpb_ex(m68_state); break; case 0xf2: sbcb_ex(m68_state); break; case 0xf3: addd_ex(m68_state); break; case 0xf4: andb_ex(m68_state); break; case 0xf5: bitb_ex(m68_state); break; case 0xf6: ldb_ex(m68_state); break; case 0xf7: stb_ex(m68_state); break; case 0xf8: eorb_ex(m68_state); break; case 0xf9: adcb_ex(m68_state); break; case 0xfa: orb_ex(m68_state); break; case 0xfb: addb_ex(m68_state); break; case 0xfc: ldd_ex(m68_state); break; case 0xfd: std_ex(m68_state); break; case 0xfe: ldu_ex(m68_state); break; case 0xff: stu_ex(m68_state); break; } #else (*m6809_main[m68_state->ireg])(m68_state); #endif /* BIG_SWITCH */ m68_state->icount -= cycles1[m68_state->ireg]; } while( m68_state->icount > 0 ); m68_state->icount -= m68_state->extra_cycles; m68_state->extra_cycles = 0; } return cycles - m68_state->icount; /* NS 970908 */ }
long gauss(mat_ZZ_p& M_in, long w) { long k, l; long i, j; long pos; ZZ t1, t2, t3; ZZ *x, *y; long n = M_in.NumRows(); long m = M_in.NumCols(); if (w < 0 || w > m) LogicError("gauss: bad args"); const ZZ& p = ZZ_p::modulus(); vec_ZZVec M; sqr(t1, p); mul(t1, t1, n); M.SetLength(n); for (i = 0; i < n; i++) { M[i].SetSize(m, t1.size()); for (j = 0; j < m; j++) { M[i][j] = rep(M_in[i][j]); } } l = 0; for (k = 0; k < w && l < n; k++) { pos = -1; for (i = l; i < n; i++) { rem(t1, M[i][k], p); M[i][k] = t1; if (pos == -1 && !IsZero(t1)) { pos = i; } } if (pos != -1) { swap(M[pos], M[l]); InvMod(t3, M[l][k], p); NegateMod(t3, t3, p); for (j = k+1; j < m; j++) { rem(M[l][j], M[l][j], p); } for (i = l+1; i < n; i++) { // M[i] = M[i] + M[l]*M[i,k]*t3 MulMod(t1, M[i][k], t3, p); clear(M[i][k]); x = M[i].elts() + (k+1); y = M[l].elts() + (k+1); for (j = k+1; j < m; j++, x++, y++) { // *x = *x + (*y)*t1 mul(t2, *y, t1); add(t2, t2, *x); *x = t2; } } l++; } } for (i = 0; i < n; i++) for (j = 0; j < m; j++) conv(M_in[i][j], M[i][j]); return l; }
void mul(mat_zz_p& X, const mat_zz_p& A, long b_in) { zz_p b; b = b_in; mul(X, A, b); }
int fact2 ( int n ) { addMoreLard(); if (n == 0) return 1; else return mul(n, fact1(n-1)); }
void determinant(zz_p& d, const mat_zz_p& M_in) { long k, n; long i, j; long pos; zz_p t1, t2, t3; zz_p *x, *y; mat_zz_p M; M = M_in; n = M.NumRows(); if (M.NumCols() != n) LogicError("determinant: nonsquare matrix"); if (n == 0) { set(d); return; } zz_p det; set(det); long p = zz_p::modulus(); mulmod_t pinv = zz_p::ModulusInverse(); for (k = 0; k < n; k++) { pos = -1; for (i = k; i < n; i++) { if (!IsZero(M[i][k])) { pos = i; break; } } if (pos != -1) { if (k != pos) { swap(M[pos], M[k]); negate(det, det); } mul(det, det, M[k][k]); inv(t3, M[k][k]); for (i = k+1; i < n; i++) { // M[i] = M[i] - M[k]*M[i,k]*t3 mul(t1, M[i][k], t3); negate(t1, t1); x = M[i].elts() + (k+1); y = M[k].elts() + (k+1); long T1 = rep(t1); mulmod_precon_t t1pinv = PrepMulModPrecon(T1, p, pinv); // T1*pinv; long T2; for (j = k+1; j < n; j++, x++, y++) { // *x = *x + (*y)*t1 T2 = MulModPrecon(rep(*y), T1, p, t1pinv); x->LoopHole() = AddMod(rep(*x), T2, p); } } } else { clear(d); return; } } d = det; }
//-- CgShaderCelScreen::CgShaderCelScreen(CGcontext cgContext) : CgShader(cgContext, CgShader::VertexFragment) { // [rad] This is vertex and fragment shader // [rad] Setup vertex shader: // [rad] Setup vertex shader entry point SetEntry(CgShader::Vertex, "main"); static const std::string sProgramVertex = " \ \ void main( float4 inPosition : POSITION, \ float2 inTexCoord : TEXCOORD0, \ \ out float4 outPosition : POSITION, \ out float2 outTexCoord : TEXCOORD0, \ \ uniform float4x4 uniModelViewProjMat) \ { \ outPosition = mul(uniModelViewProjMat, inPosition); \ outTexCoord = inTexCoord; \ } "; // [rad] Setup vertex shader program SetProgram(CgShader::Vertex, sProgramVertex); // [rad] Setup fragment shader: // [rad] Setup fragment shader entry point SetEntry(CgShader::Fragment, "main"); static const std::string sProgramFragment = " \ \ void main( float2 inTexCoord : TEXCOORD0, \ \ out float4 outColor : COLOR, \ \ uniform int2 uniTexSize, \ uniform sampler2D uniTexCel, \ uniform sampler2D uniTexOutline) \ { \ float4 cel = tex2D(uniTexCel, inTexCoord); \ float4 outline = tex2D(uniTexOutline, inTexCoord); \ \ if(outline.x == 0) \ { \ outColor = float4(0, 0, 0, 1); \ } \ else \ { \ outColor = cel; \ } \ } "; // [rad] Setup fragment shader program SetProgram(CgShader::Fragment, sProgramFragment); // [rad] Create shaders (both fragment and vertex) Create(); // [rad] Set params m_cgParamModelViewProjMatrix = cgGetNamedParameter(m_cgShaderVertex, "uniModelViewProjMat"); m_cgParamSamplerCel = cgGetNamedParameter(m_cgShaderFragment, "uniTexCel"); m_cgParamSamplerOutline = cgGetNamedParameter(m_cgShaderFragment, "uniTexOutline"); m_cgParamSamplerSize = cgGetNamedParameter(m_cgShaderFragment, "uniTexSize"); }
void inv(zz_p& d, mat_zz_p& X, const mat_zz_p& A) { long n = A.NumRows(); if (A.NumCols() != n) LogicError("inv: nonsquare matrix"); if (n == 0) { set(d); X.SetDims(0, 0); return; } long i, j, k, pos; zz_p t1, t2, t3; zz_p *x, *y; mat_zz_p M; M.SetDims(n, 2*n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { M[i][j] = A[i][j]; clear(M[i][n+j]); } set(M[i][n+i]); } zz_p det; set(det); long p = zz_p::modulus(); mulmod_t pinv = zz_p::ModulusInverse(); for (k = 0; k < n; k++) { pos = -1; for (i = k; i < n; i++) { if (!IsZero(M[i][k])) { pos = i; break; } } if (pos != -1) { if (k != pos) { swap(M[pos], M[k]); negate(det, det); } mul(det, det, M[k][k]); inv(t3, M[k][k]); M[k][k] = t3; for (i = k+1; i < n; i++) { // M[i] = M[i] - M[k]*M[i,k]*t3 mul(t1, M[i][k], t3); negate(t1, t1); x = M[i].elts() + (k+1); y = M[k].elts() + (k+1); long T1 = rep(t1); mulmod_precon_t t1pinv = PrepMulModPrecon(T1, p, pinv); // T1*pinv; long T2; for (j = k+1; j < 2*n; j++, x++, y++) { // *x = *x + (*y)*t1 T2 = MulModPrecon(rep(*y), T1, p, t1pinv); x->LoopHole() = AddMod(rep(*x), T2, p); } } } else { clear(d); return; } } X.SetDims(n, n); for (k = 0; k < n; k++) { for (i = n-1; i >= 0; i--) { clear(t1); for (j = i+1; j < n; j++) { mul(t2, X[j][k], M[i][j]); add(t1, t1, t2); } sub(t1, M[i][n+k], t1); mul(X[i][k], t1, M[i][i]); } } d = det; }
float Vertex::dot(Vertex v) { return mul(v).sum(); }
long gauss(mat_zz_p& M, long w) { long k, l; long i, j; long pos; zz_p t1, t2, t3; zz_p *x, *y; long n = M.NumRows(); long m = M.NumCols(); if (w < 0 || w > m) LogicError("gauss: bad args"); long p = zz_p::modulus(); mulmod_t pinv = zz_p::ModulusInverse(); long T1, T2; l = 0; for (k = 0; k < w && l < n; k++) { pos = -1; for (i = l; i < n; i++) { if (!IsZero(M[i][k])) { pos = i; break; } } if (pos != -1) { swap(M[pos], M[l]); inv(t3, M[l][k]); negate(t3, t3); for (i = l+1; i < n; i++) { // M[i] = M[i] + M[l]*M[i,k]*t3 mul(t1, M[i][k], t3); T1 = rep(t1); mulmod_precon_t T1pinv = PrepMulModPrecon(T1, p, pinv); clear(M[i][k]); x = M[i].elts() + (k+1); y = M[l].elts() + (k+1); for (j = k+1; j < m; j++, x++, y++) { // *x = *x + (*y)*t1 T2 = MulModPrecon(rep(*y), T1, p, T1pinv); T2 = AddMod(T2, rep(*x), p); (*x).LoopHole() = T2; } } l++; } } return l; }
template<typename T>T power(T e, T n, T m){T x=1,p=e;while(n){if(n&1)x=mul(x,p,m);p=mul(p,p,m);n>>=1;}return x;}
vec_zz_p operator*(const vec_zz_p& a, const mat_zz_p& b) { vec_zz_p res; mul(res, a, b); NTL_OPT_RETURN(vec_zz_p, res); }
RCP<const Basic> neg(const RCP<const Basic> &a) { return mul(minus_one, a); }
void inv(ZZ_p& d, mat_ZZ_p& X, const mat_ZZ_p& A) { long n = A.NumRows(); if (A.NumCols() != n) LogicError("inv: nonsquare matrix"); if (n == 0) { set(d); X.SetDims(0, 0); return; } long i, j, k, pos; ZZ t1, t2; ZZ *x, *y; const ZZ& p = ZZ_p::modulus(); vec_ZZVec M; sqr(t1, p); mul(t1, t1, n); M.SetLength(n); for (i = 0; i < n; i++) { M[i].SetSize(2*n, t1.size()); for (j = 0; j < n; j++) { M[i][j] = rep(A[i][j]); clear(M[i][n+j]); } set(M[i][n+i]); } ZZ det; set(det); for (k = 0; k < n; k++) { pos = -1; for (i = k; i < n; i++) { rem(t1, M[i][k], p); M[i][k] = t1; if (pos == -1 && !IsZero(t1)) { pos = i; } } if (pos != -1) { if (k != pos) { swap(M[pos], M[k]); NegateMod(det, det, p); } MulMod(det, det, M[k][k], p); // make M[k, k] == -1 mod p, and make row k reduced InvMod(t1, M[k][k], p); NegateMod(t1, t1, p); for (j = k+1; j < 2*n; j++) { rem(t2, M[k][j], p); MulMod(M[k][j], t2, t1, p); } for (i = k+1; i < n; i++) { // M[i] = M[i] + M[k]*M[i,k] t1 = M[i][k]; // this is already reduced x = M[i].elts() + (k+1); y = M[k].elts() + (k+1); for (j = k+1; j < 2*n; j++, x++, y++) { // *x = *x + (*y)*t1 mul(t2, *y, t1); add(*x, *x, t2); } } } else { clear(d); return; } } X.SetDims(n, n); for (k = 0; k < n; k++) { for (i = n-1; i >= 0; i--) { clear(t1); for (j = i+1; j < n; j++) { mul(t2, rep(X[j][k]), M[i][j]); add(t1, t1, t2); } sub(t1, t1, M[i][n+k]); conv(X[i][k], t1); } } conv(d, det); }
ImplicitFunction ImplicitFunctionInternal::jac(const std::vector<int> iind, int oind){ // Single output casadi_assert(oind==0); // Get the function SXFunction f = shared_cast<SXFunction>(f_); casadi_assert(!f.isNull()); // Get the jacobians Matrix<SX> Jz = f.jac(0,0); // Number of equations int nz = f.input(0).numel(); // All variables vector<Matrix<SX> > f_in(f.getNumInputs()); f_in[0] = f.inputExpr(0); for(int i=1; i<f.getNumInputs(); ++i) f_in[i] = f.inputExpr(i); // Augmented nonlinear equation Matrix<SX> F_aug = f.outputExpr(0); // Number of right hand sides int nrhs = 1; // Augment variables and equations for(vector<int>::const_iterator it=iind.begin(); it!=iind.end(); ++it){ // Get the jacobian Matrix<SX> Jx = f.jac(*it+1,0); // Number of variables int nx = f.input(*it+1).numel(); // Sensitivities Matrix<SX> dz_dx = ssym("dz_dx", nz, nx); // Derivative equation Matrix<SX> f_der = mul(Jz,dz_dx) + Jx; // Append variables f_in[0].append(vec(dz_dx)); // Augment nonlinear equation F_aug.append(vec(f_der)); // Number of right hand sides nrhs += nx; } // Augmented nonlinear equation SXFunction f_aug(f_in, F_aug); // Create new explciit function ImplicitFunction ret; ret.assignNode(create(f_aug,nrhs)); ret.setOption(dictionary()); // Return the created solver return ret; }
HUGEINT& HUGEINT::operator *= (const HUGEINT & a){ //由于a对象途中可能使用多次,因此必须判断 *this a是否是相同对象 mul(a.vData); bSymbol = (bSymbol != a.bSymbol); return *this; }
void __fastcall TForm1::Button1Click(TObject *Sender) { memset(_b,0,sizeof(_b)); memset(_a,0,sizeof(_a)); int i; int n=4; int e2=Edit2->Text.Length(); int e3=Edit3->Text.Length(); for(i=1; i<=n; i++) _a[n-i]= (AnsiString("0x0")+Edit2->Text.SubString(e2>=8*i?e2-8*i+1:1,e2>=8*i?8:(e2-8*(i-1)>0?e2-8*(i-1):0))).ToInt(); for(i=1; i<=n; i++) _a[n-i+n]= (AnsiString("0x0")+Edit3->Text.SubString(e3>=8*i?e3-8*i+1:1,e3>=8*i?8:(e3-8*(i-1)>0?e3-8*(i-1):0))).ToInt(); // n=2 /* equ(4, 1, 1); sub(4, 0, 1); equ(5, 2, 1); sub(5, 3, 1); xchg(1, 2, 1); mul(0, 1, 1); mul(2, 3, 1); mul(4, 5, 1); add(4, 0, 2); add(4, 2, 2); add(1, 4, 2); /* */ // n=4 equ(12, 2, 2); sub(12, 0, 2); equ(14, 4, 2); sub(14, 6, 2); xchg(2, 4, 2); equ(8, 1, 1); sub(8, 0, 1); equ(9, 2, 1); sub(9, 3, 1); xchg(1, 2, 1); equ(10, 5, 1); sub(10, 4, 1); equ(11, 6, 1); sub(11, 7, 1); xchg(5, 6, 1); equ(16, 13, 1); sub(16, 12, 1); equ(17, 14, 1); sub(17, 15, 1); xchg(13, 14, 1); mul(0, 1, 1); mul(2, 3, 1); mul(8, 9, 1); mul(4, 5, 1); mul(6, 7, 1); mul(10, 11, 1); mul(12, 13, 1); mul(14, 15, 1); mul(16, 17, 1); add(8, 0, 2); add(8, 2, 2); add(1, 8, 2); add(10, 4, 2); add(10, 6, 2); add(5, 10, 2); add(16, 12, 2); add(16, 14, 2); add(13, 16, 2); add(12, 0, 4); add(12, 4, 4); add(2, 12, 4); /* */ Edit4->Text=""; for(i=0; i<n+n; i++) Edit4->Text=Edit4->Text+IntToHex((int)_a[i],8); }