int main (void) { static int i; int n; printf("Please enter the maximum degree of the polynomial:\n"); scanf("%d", &n); printf("Please enter the coefficients:\n"); for(i=n; i>=0; i--){ scanf("%d", &a[i]); } printf("The polynomial is "); poly(n); printf("\nIts derivative is "); deri(n); }
int main (void) { static int i; int n; printf("Please enter the maximum degree of the polynomial:\n"); scanf("%d", &n); if(n>=0){ //run the program when n>= 0 a = (int *)calloc(n+1, sizeof(int)); printf("Please enter the coefficients:\n"); if (a != NULL) { for(i=n; i>=0; i--) { scanf("%d", &a[i]); } printf("The polynomial is "); poly(n); printf("\nIts derivative is "); deri(n); free(a); } } else //do nothing when n<0 { } }
void KNDdeTorusCollocation::jacobian(KNSparseMatrix& A, KNArray1D< KNVector* > Avar, KNVector& rhs, KNVector& par, KNVector& /*sol*/, KNArray1D<size_t>& var) { A.clear(); rhs.clear(); for (size_t r = 0; r < var.size(); r++) Avar(r)->clear(); // rhs doesn't need to be cleared // creates kk, ee, rr & interpolates p_xx & gets p_tau // init(sol, par); // builds up the structure of the sparse matrix for (size_t idx = 0; idx < time1.size(); ++idx) { const size_t lend = NDIM * (rr(ee((NTAU+1)*(ndeg1+1)*(ndeg2+1)-1,idx),idx) + 1); for (size_t p = 0; p < NDIM; p++) A.newLine(lend); } // the right-hand side sys->p_rhs(p_fx, time1, p_xx, par, 0); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) { rhs(p + NDIM*idx) = par(0) * p_fx(p,idx) - p_xx(p, NTAU, idx) - par(RHO) * p_xx(p, NTAU + 1, idx); } } // derivatives w.r.t the parameters for (size_t r = 0; r < var.size(); r++) { KNVector& deri = *Avar(r); deri.clear(); //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the period //!!!!!!!!!!!!!!!!! if (var(r) == 0) { for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = -p_fx(p,idx); } sys->p_dtau(p_dtau, time1, par, 0); for (size_t k = 0; k < NTAU; k++) { size_t nx = 1, vx = k, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { const double d = p_tau(k,idx) / par(0) - p_dtau(k,idx); deri(p + NDIM*idx) -= p_dfx(p, q, idx) * d * (p_xx(q, NTAU + 2 * (k + 1), idx) + par(RHO) * p_xx(q, NTAU + 2 * (k + 1) + 1, idx)); } } } } } else //!!!!!!!!!!!!!!!!! //!END w.r.t the period //!!!!!!!!!!!!!!!!! // //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the ordinary parameters //!!!!!!!!!!!!!!!!! // derivatives w.r.t. the real parameters if (var(r) < NPAR) { size_t nx = 0, vx = 0, np = 1, vp = var(r); sys->p_deri(p_dfp, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = - par(0) * p_dfp(p, 0, idx); } } else //!!!!!!!!!!!!!!!!! //!END w.r.t the ordinary parameters //!!!!!!!!!!!!!!!!! // //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the rotation number //!!!!!!!!!!!!!!!!! if (var(r) == RHO) { for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = p_xx(p, NTAU + 1, idx); } sys->p_dtau(p_dtau, time1, par, 0); for (size_t k = 0; k < NTAU; k++) { const size_t nx = 1, vx = k, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { const double d = -p_tau(k, idx); for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { deri(p + NDIM*idx) -= p_dfx(p, q, idx) * d * p_xx(q, NTAU + 2 * (k + 1) + 1, idx); } } } } } //!!!!!!!!!!!!!!!!! //!END w.r.t the rotation number //!!!!!!!!!!!!!!!!! else std::cout << "Jac:NNN\n"; } // fill the matrix for (size_t k = 0; k < NTAU + 1; k++) { if (k != 0) { // evaluate the solution const size_t nx = 1, vx = k - 1, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); } for (size_t idx = 0; idx < time1.size(); ++idx) { double tk1, tk2; if (k != 0) { tk1 = time1(idx) - p_tau(k-1,idx) / par(0); tk2 = time2(idx) - par(RHO) * p_tau(k-1,idx) / par(0); } else { tk1 = time1(idx); tk2 = time2(idx); } const double c1 = nint1 * tk1 - floor(nint1 * tk1); const double c2 = nint2 * tk2 - floor(nint2 * tk2); // the real jacobian for (size_t l2 = 0; l2 < ndeg2 + 1; l2++) { for (size_t l1 = 0; l1 < ndeg1 + 1; l1++) { const size_t idxK = idxkk(l1, l2, k); if (k != 0) { const double cf = poly_lgr_eval(mesh1, l1, c1) * poly_lgr_eval(mesh2, l2, c2); for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { // jacobian of rhs A.writeIndex(p + NDIM*idx, q + NDIM*rr(idxK, idx)) = static_cast<int>(q + NDIM * kk(idxK, idx)); A.writeData(p + NDIM*idx, q + NDIM*rr(idxK, idx)) -= cf * par(0) * p_dfx(p, q, idx); } } } else { const double cf1 = poly_dlg_eval(mesh1, l1, c1) * nint1 * poly_lgr_eval(mesh2, l2, c2); const double cf2 = poly_lgr_eval(mesh1, l1, c1) * poly_dlg_eval(mesh2, l2, c2) * nint2; for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { // derivative part of the jacobian A.writeIndex(p + NDIM*idx, q + NDIM*rr(idxK, idx)) = static_cast<int>(q + NDIM * kk(idxK, idx)); if (p == q) A.writeData(p + NDIM*idx, q + NDIM*rr(idxK, idx)) += cf1 + par(RHO) * cf2; } } } // error check if (kk(idxK, idx) > (ndeg1*nint1)*(ndeg2*nint2)) std::cout << "D" << kk(idxK, idx); } } } } }