void test6(void) { ElementNode_handle pE1=0,pE2=0,pE3=0; int i; printf("\n==== SCALAR MULTIPLICATION =================\n"); for(i=0;i<20;++i) insert_element(&pE1,i,i*i); printf("vector pE1 formatted:\n"); printf_elements(pE1,"%4d",20); printf("\n"); for(i=0;i<20;++i) insert_element(&pE2,40-2*i,i); printf("vector pE2 formatted:\n"); printf_elements(pE2,"%4d",40); printf("\n"); for(i=0;i<20;++i) { if (i%2) insert_element(&pE3,2*i,i); else insert_element(&pE3,41-2*i,-i); } printf("vector pE3 formatted:\n"); printf_elements(pE3,"%4d",40); printf("\n"); printf("scalar product pE1 and pE2 = %i\n",scalar_product(pE1,pE2)); printf("scalar product pE2 and pE3 = %i\n",scalar_product(pE2,pE3)); printf("scalar product pE3 and pE1 = %i\n",scalar_product(pE3,pE1)); printf("scalar product pE1 and pE1 = %i\n",scalar_product(pE1,pE1)); free_elements(pE1); free_elements(pE2); free_elements(pE3); }
bool collisionLineCircle( const Circle& circle, const Line& l ) noexcept { const FloatPosition A = l.o; const FloatPosition B = { l.o.x + l.v.vx, l.o.y + l.v.vy }; if ( collisionPointCircle( A, circle ) || collisionPointCircle( B, circle ) ) return true; const FloatPosition O = circle.center; const Vector2D AB = {B.x - A.x, B.y - A.y}; const Vector2D AO = {O.x - A.x, O.y - A.y}; const Vector2D BO = {O.x - B.x, O.y - B.y}; // Using the opposite value of vx for scal2 const Float& scal_ab_ao = scalar_product( AB, AO ); const Float& scal_mab_bo = ( ( -AB.vx ) * BO.vx ) + ( ( -AB.vy ) * BO.vy ); if ( scal_ab_ao < FNIL || scal_mab_bo < FNIL ) return false; // Find the projection point of O const Float& scalp = scalar_product( AB, AB ); if ( scalp == FNIL ) // A and B are the same point return false; const Float& T = scal_ab_ao / scalp; const Float& x = A.x + ( T * AB.vx ); const Float& y = A.y + ( T * AB.vy ); // Ok I can calculate the collision by using ↓ the projection point return collisionPointCircle( FloatPosition{x, y}, circle ); }
t_ray find_refract_vect(t_ray *start_ray, t_hit drawn_pixel, double c_r, int test) { double ref_refract; double new_ref_index; t_ray res; t_vec new_norm; t_vec inv_dir; res.pos = vec_add(start_ray->pos, scalar_product(start_ray->dir, drawn_pixel.t)); inv_dir = scalar_product(start_ray->dir, -1); drawn_pixel.point_norm = scalar_product(drawn_pixel.point_norm, test); ref_refract = dot_product(drawn_pixel.point_norm, inv_dir); ref_refract /= (get_length(drawn_pixel.point_norm) * get_length(inv_dir)); new_ref_index = 1 - c_r * c_r * (1 - ref_refract * ref_refract); if (new_ref_index > 0) { new_ref_index = sqrt(new_ref_index); new_ref_index = c_r * ref_refract - new_ref_index; new_norm = scalar_product(drawn_pixel.point_norm, new_ref_index); res.dir = scalar_product(inv_dir, c_r); res.dir = vec_sub(res.dir, new_norm); } else res.dir = init_vector(0, 0, 0); return (res); }
void test7( void ) { ElementNode * pE1=0, *pE2=0; int i; printf( "\n==== SCALAR MULTIPLICATION 2 =================\n" ); for( i=0; i<20; ++i ) insert_element( &pE1,1000*i,i*i ); for( i=0; i<20; ++i ) insert_element( &pE2,500*i,i ); printf( "scalar product pE1 and pE2 = %i\n",scalar_product( pE1,pE2 ) ); printf( "scalar product pE2 and pE1 = %i\n",scalar_product( pE2,pE1 ) ); printf( "scalar product pE1 and pE1 = %i\n",scalar_product( pE1,pE1 ) ); free_elements( pE1 ); free_elements( pE2 ); }
/* checks if a vector is a feasible point of the LP */ int is_Solution(bip inst, int* test){ assert(is_initialized(inst)); for (int i=0; i<inst.rows; ++i){ if (scalar_product(get_elem(inst, i, 0),test, inst.columns)>inst.vector[i]) return 0; } return 1; }
void main() { int u[5] = {0, 1, 2, 3, 4}; int v[5] = {0, 1, 2, 3, 4}; print("[CHKPT3]: scalar_product (30) = ", scalar_product(u,v,5)); println(); }
RowNode_handle mult( ConstRowNode_handle p_r1, ConstRowNode_handle p_r2 ) { RowNode_handle transpose2 = transpose(p_r2); RowNode_handle multiplication =0, p2 = transpose2; ConstElementNode_handle columns1 = 0, columns2 = 0; int product = 0; while(p_r1) { columns1 = p_r1->elements; p2 = transpose2; while(p2) { columns2 = p2->elements; product = scalar_product(columns1, columns2); insert_element2(&multiplication, p_r1->pos, p2->pos, product); p2 = p2->next; } p_r1 = p_r1->next; } return multiplication; }
int main() { float a[N]; float b[N]; float res[1]; float golden_res = 0.0; unsigned int i, j, errors = 0; for(i = 0; i < N; i++) { a[i] = i*1.0; b[i] = i*2.0; golden_res += a[i]* b[i]; } scalar_product(a, b, res); if(res[0] == golden_res) { printf("Test succeeded!\n"); return 0; } else { printf("Test failed, expected: %f, got: %f \n", golden_res, res[0]); return 1; } }
int main() { vector v1 = { 2, 4, 6 }; vector v2 = { 3, 5, 9 }; vector v; v = vector_product(v1, v2); printf("v1 * v2 = %.2f\n", scalar_product(v1, v2)); printf("v1 x v2 = [%.2f, %.2f, %.2f]\n", v.x, v.y, v.z); return 0; }
int is_Solution_eq(bip inst, int* test){ if (!is_initialized(inst)) { return 0; } for (int i=0; i<inst.rows; ++i){ if (scalar_product(get_elem(inst, i, 0),test, inst.columns)!=inst.vector[i]) return 0; } return 1; }
/* This function checks if two vectors are orthogonal * returns -1 if they can't be checked * returns 0 if they aren't orthogonal * returns 1 if they are orthogonal */ int check_orthogonal(struct vector v1, struct vector v2) { if(v1.dimension!=v2.dimension) { printf("These vectors can't be compared\n"); printf("They don't have the same size\n"); return -1; } return !scalar_product(v1, v2); }
/* This function assumes the following: * - s1's length is inferior to s2's length * - s2 has been padded with 'len' initial and trailing zeroes * The output is a normalized cross correlation. **/ static int compute_cross_correlation(int16_t *s1, int n1, int16_t *s2_padded, float *xcorr, int xcorr_nsamples, ProgressContext *pctx, int step, int64_t *s1_energy){ int max_index = 0; int i; int64_t tmp,max=0; int64_t norm1 = scalar_product(s1, s1, n1, step); int64_t norm2 = scalar_product(s2_padded, s2_padded, n1, step) - s2_padded[step*(n1-1)]*s2_padded[step*(n1-1)]; for (i=0; i<xcorr_nsamples; i++){ norm2 += s2_padded[step*(i+n1-1)]*s2_padded[step*(i+n1-1)]; tmp = scalar_product(s1, s2_padded + i*step, n1, step); xcorr[i] = (float)((double)tmp / sqrt((double)(norm1)*(double)norm2)); tmp = tmp < 0 ? -tmp : tmp; if (tmp > max){ max = tmp; max_index = i; } norm2 -= s2_padded[step*i]*s2_padded[step*i]; progress_context_update(pctx, 100 * i/xcorr_nsamples); } if (s1_energy) *s1_energy = norm1; return max_index; }
struct vector projection_along(struct vector v1, struct vector v2) { if(v1.dimension!=v2.dimension) { printf("These vectors doesn't have the same size\n"); printf("Imposible calculate the projection"); } struct vector w=unitary_vector(v1); double aux=scalar_product(v1,v2)/module(v1); w=multiply_vector_by(v1,aux); return w; }
int main(int argc, char** argv){ if (argc < 6) { printf("Nombre de param\212tres insuffisants"); return 1; } int[] scalar_product(v1, v2, dim); return 0; }
void constr_scale_in_direction(float A[3], float B[3], float C[3], float D[3], float E[3], float F[3], float fixed_point[3], int g) /* scales group by |AB|/|CD| in direction EF */ { double lAB,lCD,s; float AB[3], CD[3], EF[3], v[3], tmp[3]; double sp; int i; if(vector_eq(E,F)) { printf("scaling in direction EF refused: EF = 0 !!!\n"); return; } vector_sub(B, A, AB); vector_sub(D, C, CD); vector_sub(F, E, EF); vector_normalize(EF); lAB=vector_length(AB); lCD=vector_length(CD); if(lCD==0) { printf("scaling refused: |CD| = 0 !!!\n"); return; } s=lAB/lCD; if(s== 1.0) { printf("scaling by 1 does not change anything !!!\n"); return; } backup(); for(i=0; i<VERTEX_MAX; i++) if(vertex_used[i] && group[i]==g) { vector_sub(vertex[i], fixed_point, v); sp=scalar_product(EF,v); vectorcpy(tmp, EF); vector_scale((s-1)*sp, tmp); vector_add(v,tmp, v); vector_add(v, fixed_point, vertex[i]); } }
static void assert_symmetry(linear_map_t A, int n, void *e, int ntrials) { int sn = n * sizeof(double); double *x = xmalloc(sn); double *y = xmalloc(sn); double *Ax = xmalloc(sn); double *Ay = xmalloc(sn); for (int i = 0; i < ntrials; i++) { fill_random_vector(x, n); fill_random_vector(y, n); A(Ax, x, n, e); A(Ay, y, n, e); double Axy = scalar_product(Ax, y, n); double xAy = scalar_product(x, Ay, n); double error = fabs(Axy-xAy); fprintf(stderr, "error symmetry = %g\n", error); if (error > 1e-12) fprintf(stderr, "WARNING: non symmetric!\n"); } free(x); free(y); free(Ax); free(Ay); }
int vertex_find_nearest(float v[]) { int i, found=-1; float tmp[3], dist, dist1; for(i=0; i<VERTEX_MAX; i++) if(vertex_used[i]>0) { vector_sub(v, vertex[i], tmp); dist1=scalar_product(tmp,tmp); if(found==-1 || dist1<dist) { found=i; dist=dist1; } } return found ; }
bool check_collinearity(const Vector& v) const { const auto v1_len = vect.size(); const auto v2_len = v.vect.size(); if (v1_len != v2_len) { return false; } const double len_prod = vector_length() * v.vector_length(); const double sc_prod = scalar_product(v); if (sc_prod == len_prod || sc_prod == -len_prod) { return true; } return false; }
static void assert_positivity(linear_map_t A, int n, void *e, int ntrials) { int sn = n * sizeof(double); double *x = xmalloc(sn); double *Ax = xmalloc(sn); for (int i = 0; i < ntrials; i++) { fill_random_vector(x, n); A(Ax, x, n, e); double Axx = scalar_product(Ax, x, n); double pos = Axx; fprintf(stderr, "positivity = %g\n", pos); if (pos < -1e-12) fprintf(stderr, "WARNING: non positive!\n"); } free(x); free(Ax); }
EXPORT void oblique_propagate_at_node( Front *fr, POINTER wave, POINT *newp, O_CURVE *oldc, O_CURVE *newc, double *nor, double dt) { BOND *oldb; POINT *oldn_posn, *oldp; double save[MAXD], dposn[MAXD]; double len, t[MAXD]; double V[MAXD]; int i, dim = fr->rect_grid->dim; void (*save_impose_bc)(POINT*,BOND*,CURVE*,double*,Front*, boolean,boolean); oldb = Bond_at_node_of_o_curve(oldc); oldn_posn = Node_of_o_curve(oldc)->posn; oldp = Point_adjacent_to_node(oldc->curve,oldc->orient); for (i = 0; i < dim; ++i) { save[i] = Coords(oldp)[i]; dposn[i] = save[i] - Coords(oldn_posn)[i]; } t[0] = -nor[1]; t[1] = nor[0]; len = mag_vector(t,dim); for (i = 0; i < dim; ++i) t[i] /= len; if (scalar_product(t,dposn,dim) < 0.0) { for (i = 0; i < dim; ++i) t[i] = -t[i]; } len = 0.0; for (i = 0; i < dim; ++i) len += fabs(t[i]*fr->rect_grid->h[i]); for (i = 0; i < dim; ++i) Coords(oldp)[i] = Coords(oldn_posn)[i] + len * t[i]; save_impose_bc = fr->impose_bc; fr->impose_bc = NULL; point_propagate(fr,wave,oldn_posn,newp,oldb,oldc->curve,dt,V); if (newc->orient != oldc->orient) reverse_states_at_point(newp,fr); for (i = 0; i < dim; ++i) Coords(oldp)[i] = save[i]; fr->impose_bc = save_impose_bc; } /*end oblique_propagate_at_node*/
void COpenGLView::TrackBall(CPoint last, CPoint init, double& angle, double& x, double& y, double& z) { Point3d end, start, out; double sum, len, norm; double pi = 3.1415926535; norm = (double)__min(m_width, m_height); start.x = (2*init.x-m_width)/norm; start.y = (m_height-2.*init.y)/norm; sum = start.x*start.x + start.y*start.y; if (sum < 1.) start.z = sqrt(1 - sum); else start.z = 0.; end.x = (2.*last.x-m_width)/norm; end.y = (m_height-2.*last.y)/norm; sum = end.x*end.x + end.y*end.y; if (sum < 1.) end.z = sqrt(1 - sum); else end.z = 0.; ASSERT(vector_length(start)*vector_length(end)>1.0e-30); angle = scalar_product(start,end)/(vector_length(start)*vector_length(end)); if (angle<=-1) angle = -0.99999; if (angle>=1) angle = 0.99999; angle = acos(angle)*180./pi; out.x = vector_product_x(start,end); out.y = vector_product_y(start,end); out.z = vector_product_z(start,end); len = vector_length(out); if (len != 0.) { out.x /= len; out.y /= len; out.z /= len; } x = out.x; y = out.y; z = out.z; }
/* * transforms vectors by using Gram-Schmidt orthogonalization algorithm * * @parameter vector : list of vectors * @parameter m : number of vectors * @parameter n : dimension of vectors (m <= n) */ void gram_schmidt_orthogonalization ( double** vector, int m, int n) { if (m <= 1) return; for (int i = 0; i < m; i++) { swap_maxlen_to_head(vector+i, m-i, n); for (int j = i+1; j < m; j++) { double alpha = scalar_product(vector[i], vector[j], n); double length = get_length(vector[i], n); /* check length > 0 (if length=0, then alpha=0) */ if (isnormal(length)) { alpha /= length * length; } for (int k = 0; k < n; k++) { vector[j][k] -= alpha * vector[i][k]; } } } }
int orthogonalize(deque<double> & a, deque<deque<double> > & M) { Euclidean_normalize(a); for(int i=0; i<M.size(); i++) { double w= scalar_product(a, M[i]); for(int j=0; j<a.size(); j++) a[j]-=w*M[i][j]; } Euclidean_normalize(a); return 0; }
double QuadProg::solve_quadprog(Matrix<double>& G, Vector<double>& g0, const Matrix<double>& CE, const Vector<double>& ce0, const Matrix<double>& CI, const Vector<double>& ci0, Vector<double>& x) { std::ostringstream msg; { //Ensure that the dimensions of the matrices and vectors can be //safely converted from unsigned int into to int without overflow. unsigned mx = std::numeric_limits<int>::max(); if(G.ncols() >= mx || G.nrows() >= mx || CE.nrows() >= mx || CE.ncols() >= mx || CI.nrows() >= mx || CI.ncols() >= mx || ci0.size() >= mx || ce0.size() >= mx || g0.size() >= mx){ msg << "The dimensions of one of the input matrices or vectors were " << "too large." << std::endl << "The maximum allowable size for inputs to solve_quadprog is:" << mx << std::endl; throw std::logic_error(msg.str()); } } int n = G.ncols(), p = CE.ncols(), m = CI.ncols(); if ((int)G.nrows() != n) { msg << "The matrix G is not a square matrix (" << G.nrows() << " x " << G.ncols() << ")"; throw std::logic_error(msg.str()); } if ((int)CE.nrows() != n) { msg << "The matrix CE is incompatible (incorrect number of rows " << CE.nrows() << " , expecting " << n << ")"; throw std::logic_error(msg.str()); } if ((int)ce0.size() != p) { msg << "The vector ce0 is incompatible (incorrect dimension " << ce0.size() << ", expecting " << p << ")"; throw std::logic_error(msg.str()); } if ((int)CI.nrows() != n) { msg << "The matrix CI is incompatible (incorrect number of rows " << CI.nrows() << " , expecting " << n << ")"; throw std::logic_error(msg.str()); } if ((int)ci0.size() != m) { msg << "The vector ci0 is incompatible (incorrect dimension " << ci0.size() << ", expecting " << m << ")"; throw std::logic_error(msg.str()); } x.resize(n); register int i, j, k, l; /* indices */ int ip; // this is the index of the constraint to be added to the active set Matrix<double> R(n, n), J(n, n); Vector<double> s(m + p), z(n), r(m + p), d(n), np(n), u(m + p), x_old(n), u_old(m + p); double f_value, psi, c1, c2, sum, ss, R_norm; double inf; if (std::numeric_limits<double>::has_infinity) inf = std::numeric_limits<double>::infinity(); else inf = 1.0E300; double t, t1, t2; /* t is the step lenght, which is the minimum of the partial step length t1 * and the full step length t2 */ Vector<int> A(m + p), A_old(m + p), iai(m + p); int q, iq, iter = 0; Vector<bool> iaexcl(m + p); /* p is the number of equality constraints */ /* m is the number of inequality constraints */ q = 0; /* size of the active set A (containing the indices of the active constraints) */ #ifdef TRACE_SOLVER std::cout << std::endl << "Starting solve_quadprog" << std::endl; print_matrix("G", G); print_vector("g0", g0); print_matrix("CE", CE); print_vector("ce0", ce0); print_matrix("CI", CI); print_vector("ci0", ci0); #endif /* * Preprocessing phase */ /* compute the trace of the original matrix G */ c1 = 0.0; for (i = 0; i < n; i++) { c1 += G[i][i]; } /* decompose the matrix G in the form L^T L */ cholesky_decomposition(G); #ifdef TRACE_SOLVER print_matrix("G", G); #endif /* initialize the matrix R */ for (i = 0; i < n; i++) { d[i] = 0.0; for (j = 0; j < n; j++) R[i][j] = 0.0; } R_norm = 1.0; /* this variable will hold the norm of the matrix R */ /* compute the inverse of the factorized matrix G^-1, this is the initial value for H */ c2 = 0.0; for (i = 0; i < n; i++) { d[i] = 1.0; forward_elimination(G, z, d); for (j = 0; j < n; j++) J[i][j] = z[j]; c2 += z[i]; d[i] = 0.0; } #ifdef TRACE_SOLVER print_matrix("J", J); #endif /* c1 * c2 is an estimate for cond(G) */ /* * Find the unconstrained minimizer of the quadratic form 0.5 * x G x + g0 x * this is a feasible point in the dual space * x = G^-1 * g0 */ cholesky_solve(G, x, g0); for (i = 0; i < n; i++) x[i] = -x[i]; /* and compute the current solution value */ f_value = 0.5 * scalar_product(g0, x); #ifdef TRACE_SOLVER std::cout << "Unconstrained solution: " << f_value << std::endl; print_vector("x", x); #endif /* Add equality constraints to the working set A */ iq = 0; for (i = 0; i < p; i++) { for (j = 0; j < n; j++) np[j] = CE[j][i]; compute_d(d, J, np); update_z(z, J, d, iq); update_r(R, r, d, iq); #ifdef TRACE_SOLVER print_matrix("R", R, n, iq); print_vector("z", z); print_vector("r", r, iq); print_vector("d", d); #endif /* compute full step length t2: i.e., the minimum step in primal space s.t. the contraint becomes feasible */ t2 = 0.0; if (fabs(scalar_product(z, z)) > std::numeric_limits<double>::epsilon()) // i.e. z != 0 t2 = (-scalar_product(np, x) - ce0[i]) / scalar_product(z, np); /* set x = x + t2 * z */ for (k = 0; k < n; k++) x[k] += t2 * z[k]; /* set u = u+ */ u[iq] = t2; for (k = 0; k < iq; k++) u[k] -= t2 * r[k]; /* compute the new solution value */ f_value += 0.5 * (t2 * t2) * scalar_product(z, np); A[i] = -i - 1; if (!add_constraint(R, J, d, iq, R_norm)) { // Equality constraints are linearly dependent throw std::runtime_error("Constraints are linearly dependent"); return f_value; } } /* set iai = K \ A */ for (i = 0; i < m; i++) iai[i] = i; l1: iter++; #ifdef TRACE_SOLVER print_vector("x", x); #endif /* step 1: choose a violated constraint */ for (i = p; i < iq; i++) { ip = A[i]; iai[ip] = -1; } /* compute s[x] = ci^T * x + ci0 for all elements of K \ A */ ss = 0.0; psi = 0.0; /* this value will contain the sum of all infeasibilities */ ip = 0; /* ip will be the index of the chosen violated constraint */ for (i = 0; i < m; i++) { iaexcl[i] = true; sum = 0.0; for (j = 0; j < n; j++) sum += CI[j][i] * x[j]; sum += ci0[i]; s[i] = sum; psi += std::min(0.0, sum); } #ifdef TRACE_SOLVER print_vector("s", s, m); #endif if (fabs(psi) <= m * std::numeric_limits<double>::epsilon() * c1 * c2* 100.0) { /* numerically there are not infeasibilities anymore */ q = iq; return f_value; } /* save old values for u and A */ for (i = 0; i < iq; i++) { u_old[i] = u[i]; A_old[i] = A[i]; } /* and for x */ for (i = 0; i < n; i++) x_old[i] = x[i]; l2: /* Step 2: check for feasibility and determine a new S-pair */ for (i = 0; i < m; i++) { if (s[i] < ss && iai[i] != -1 && iaexcl[i]) { ss = s[i]; ip = i; } } if (ss >= 0.0) { q = iq; return f_value; } /* set np = n[ip] */ for (i = 0; i < n; i++) np[i] = CI[i][ip]; /* set u = [u 0]^T */ u[iq] = 0.0; /* add ip to the active set A */ A[iq] = ip; #ifdef TRACE_SOLVER std::cout << "Trying with constraint " << ip << std::endl; print_vector("np", np); #endif l2a:/* Step 2a: determine step direction */ /* compute z = H np: the step direction in the primal space (through J, see the paper) */ compute_d(d, J, np); update_z(z, J, d, iq); /* compute N* np (if q > 0): the negative of the step direction in the dual space */ update_r(R, r, d, iq); #ifdef TRACE_SOLVER std::cout << "Step direction z" << std::endl; print_vector("z", z); print_vector("r", r, iq + 1); print_vector("u", u, iq + 1); print_vector("d", d); print_vector("A", A, iq + 1); #endif /* Step 2b: compute step length */ l = 0; /* Compute t1: partial step length (maximum step in dual space without violating dual feasibility */ t1 = inf; /* +inf */ /* find the index l s.t. it reaches the minimum of u+[x] / r */ for (k = p; k < iq; k++) { if (r[k] > 0.0) { if (u[k] / r[k] < t1) { t1 = u[k] / r[k]; l = A[k]; } } } /* Compute t2: full step length (minimum step in primal space such that the constraint ip becomes feasible */ if (fabs(scalar_product(z, z)) > std::numeric_limits<double>::epsilon()) // i.e. z != 0 t2 = -s[ip] / scalar_product(z, np); else t2 = inf; /* +inf */ /* the step is chosen as the minimum of t1 and t2 */ t = std::min(t1, t2); #ifdef TRACE_SOLVER std::cout << "Step sizes: " << t << " (t1 = " << t1 << ", t2 = " << t2 << ") "; #endif /* Step 2c: determine new S-pair and take step: */ /* case (i): no step in primal or dual space */ if (t >= inf) { /* QPP is infeasible */ // FIXME: unbounded to raise q = iq; return inf; } /* case (ii): step in dual space */ if (t2 >= inf) { /* set u = u + t * [-r 1] and drop constraint l from the active set A */ for (k = 0; k < iq; k++) u[k] -= t * r[k]; u[iq] += t; iai[l] = l; delete_constraint(R, J, A, u, n, p, iq, l); #ifdef TRACE_SOLVER std::cout << " in dual space: " << f_value << std::endl; print_vector("x", x); print_vector("z", z); print_vector("A", A, iq + 1); #endif goto l2a; } /* case (iii): step in primal and dual space */ /* set x = x + t * z */ for (k = 0; k < n; k++) x[k] += t * z[k]; /* update the solution value */ f_value += t * scalar_product(z, np) * (0.5 * t + u[iq]); /* u = u + t * [-r 1] */ for (k = 0; k < iq; k++) u[k] -= t * r[k]; u[iq] += t; #ifdef TRACE_SOLVER std::cout << " in both spaces: " << f_value << std::endl; print_vector("x", x); print_vector("u", u, iq + 1); print_vector("r", r, iq + 1); print_vector("A", A, iq + 1); #endif if (fabs(t - t2) < std::numeric_limits<double>::epsilon()) { #ifdef TRACE_SOLVER std::cout << "Full step has taken " << t << std::endl; print_vector("x", x); #endif /* full step has taken */ /* add constraint ip to the active set*/ if (!add_constraint(R, J, d, iq, R_norm)) { iaexcl[ip] = false; delete_constraint(R, J, A, u, n, p, iq, ip); #ifdef TRACE_SOLVER print_matrix("R", R); print_vector("A", A, iq); print_vector("iai", iai); #endif for (i = 0; i < m; i++) iai[i] = i; for (i = p; i < iq; i++) { A[i] = A_old[i]; u[i] = u_old[i]; iai[A[i]] = -1; } for (i = 0; i < n; i++) x[i] = x_old[i]; goto l2; /* go to step 2 */ } else iai[ip] = -1; #ifdef TRACE_SOLVER print_matrix("R", R); print_vector("A", A, iq); print_vector("iai", iai); #endif goto l1; } /* a patial step has taken */ #ifdef TRACE_SOLVER std::cout << "Partial step has taken " << t << std::endl; print_vector("x", x); #endif /* drop constraint l */ iai[l] = l; delete_constraint(R, J, A, u, n, p, iq, l); #ifdef TRACE_SOLVER print_matrix("R", R); print_vector("A", A, iq); #endif /* update s[ip] = CI * x + ci0 */ sum = 0.0; for (k = 0; k < n; k++) sum += CI[k][ip] * x[k]; s[ip] = sum + ci0[ip]; #ifdef TRACE_SOLVER print_vector("s", s, m); #endif goto l2a; }
/* * Multiply a toeplitz matrix with a vector (convolution). * IMP - The t_components have to be given in reverse order and the output is in * reverse order. */ static void toeplitz_mult(size_t size, fftw_complex *t_components, fftw_complex *vec, fftw_complex *result) { size_t dsize = size; while(dsize--) *result++ = scalar_product(size, t_components++, vec); }
// evaluate a polynomial of degree 3 in 3 variables static long double eval_pol20l(long double c[20], long double X[3]) { long double m[20]; eval_mon20(m, X); return scalar_product(m, c, 20); }
int main(int argc, char* argv[]) { // Initialization of the network, the communicator and the allocation of // the GPU is done as in previous tutorials. agile::NetworkEnvironment environment(argc, argv); typedef agile::GPUCommunicator<unsigned, float, float> communicator_type; communicator_type com; com.allocateGPU(); agile::GPUEnvironment::printInformation(std::cout); std::cout << std::endl; // We are interested in solving the linear problem \f$ Ax = y \f$, with a // given matrix \f$ A \f$ and a right-hand side vector \f$ y \f$. The unknown // is the vector \f$ x \f$. // Now, we can generate a matrix that shall be inverted (actually we do not // invert the matrix but use the CG algorithm). Note that CG requires a // symmetric positive definite (SPD) matrix and it is not too trivial to // write down a SPD matrix. If you fail to provide a SPD matrix to the CG // algorithm there is no guarantee that it will converge. You might be lucky, // you might be not... const unsigned SIZE = 20; float A_host[SIZE][SIZE]; for (unsigned row = 0; row < SIZE; ++row) for (unsigned column = 0; column <= row; ++column) { A_host[row][column] = (float(SIZE) - float(row) + float(SIZE) / 2.0) * (float(column) + 1.0); A_host[column][row] = A_host[row][column]; if (row == column) A_host[row][column] = 2.0 * float(SIZE) + float(row) + float(column); } // The matrix is still in the host's memory and has to be transfered to the // GPU. This is done automatically by the constructor of \p GPUMatrixPitched. agile::GPUMatrixPitched<float> A(SIZE, SIZE, (float*)A_host); // Next we need a reference solution. We can create any vector we like at // this place. std::vector<float> x_reference_host(SIZE); for (unsigned counter = 0; counter < SIZE; ++counter) x_reference_host[counter] = float(SIZE) - float(counter) + float(SIZE/3); // This vector has to be transfered to the GPU memory too. For vectors, this // can be achieved by the member function \p assignFromHost. agile::GPUVector<float> x_reference; x_reference.assignFromHost(x_reference_host.begin(), x_reference_host.end()); // We wrap the GPU matrix from above into a forward operator called // \p ForwardMatrix. Forward operators are simply objects that implement // the parenthesis-operator \p operator() which takes an // \p accumulated vector and returns a \p distributed one. In all other // respects the operator is a black box for us. // The \p ForwardMatrix operator requires a reference to the communicator // when constructing the object so that it has access to the network. typedef agile::ForwardMatrix<communicator_type, agile::GPUMatrixPitched<float> > forward_type; forward_type forward(com, A); // What we also want to use a preconditioner, which means that we change from // the original problem \f$ Ax = y \f$ to the equivalent one // \f$ PAx = Py \f$, where \f$ P \f$ is a preconditioner. The rationale is // that most often the matrix \f$ A \f$ is ill-conditioned and the CG algorithm // does not converge properly at all or it needs many iterations. The use of // a preconditioner makes the whole system better conditioned. The simplest // choice is to use the identity \f$ P = I \f$ (which means no preconditioning // at all). The best choice would be \f$ P = A^{-1} \f$ as we would have the // solution for \f$ x \f$ in the first step already (but then we need again // to find the inverse of \f$ A \f$ which we wanted to avoid). An // 'intermediate' possibility is to take \f$ P = diag(A)^{-1} \f$ which is // easy and fast to invert and gives better results than the identity. // A preconditioner belongs to the inverse operator. All inverse operators // implement a parenthesis-operator which takes a \p distributed vector // as input and returns an \p accumulated one (opposite to the forward // operators, thus). #if JACOBI_PRECONDITIONER typedef agile::JacobiPreconditioner<communicator_type, float> preconditioner_type; std::vector<float> diagonal(SIZE); for (unsigned row = 0; row < SIZE; ++row) diagonal[row] = A_host[row][row]; preconditioner_type preconditioner(com, diagonal); #else typedef agile::InverseIdentity<communicator_type> preconditioner_type; preconditioner_type preconditioner(com); #endif // The last operator needed is a measure. A measure operator has again // a parenthesis-operator. This timeis takes an \p accumulated vector as first // input and a \p distributed one as second input and returns a scalar // measuring somehow the size of the vectors. An example is the scalar // product operator. typedef agile::ScalarProductMeasure<communicator_type> measure_type; measure_type scalar_product(com); // Finally, generate the PCG solver. It needs the absolute and relative // tolerances as input so that it knows when the solution is good enough for // our purposes. Furthermore it requires the maximum amount of iterations // after which it simply capitulates without having found a solution. const double REL_TOLERANCE = 1e-12; const double ABS_TOLERANCE = 1e-6; const unsigned MAX_ITERATIONS = 100; agile::PreconditionedConjugateGradient<communicator_type, forward_type, preconditioner_type, measure_type> pcg(com, forward, preconditioner, scalar_product, REL_TOLERANCE, ABS_TOLERANCE, MAX_ITERATIONS); // What we have not generated, yet, is the right hand side \f$ y \f$. This is // simply one call to our forward operator. agile::GPUVector<float> y(SIZE); forward(x_reference, y); // We need one more vector to hold the result of the CG algorithm. Note that // we also supply the initial guess for the solution via this vector. agile::GPUVector<float> x(SIZE); // Finally, we have constructed, initialized, wrapped... everything. The only // thing left to do is to call the CG operator. pcg(y, x); // Print some statistics (and hope that the operator actually converged). if (pcg.convergence()) std::cout << "CG converged in "; else std::cout << "Error: CG did not converge in "; std::cout << pcg.getIteration() + 1 << " iterations." << std::endl; std::cout << "Initial residual = " << pcg.getRho0() << std::endl; std::cout << "Final residual = " << pcg.getRho() << std::endl; std::cout << "Ratio rho_k / rho_0 = " << pcg.getRho() / pcg.getRho0() << std::endl; // As the vectors in this example were quite small we can even print them to // standard output. std::cout << "Reference: " << std::endl << " "; for (unsigned counter = 0; counter < x_reference_host.size(); ++counter) std::cout << x_reference_host[counter] << " "; std::cout << std::endl; // The solution is still on the GPU and has to be transfered to the CPU memory. // This is accomplished using \p copyToHost. std::vector<float> x_host; x.copyToHost(x_host); // Output the solution, too. std::cout << "CG solution: " << std::endl << " "; for (unsigned counter = 0; counter < x_host.size(); ++counter) std::cout << x_host[counter] << " "; std::cout << std::endl; // Finally, we also compute the difference between the reference solution and // the true solution (of course, we do this on the GPU). agile::GPUVector<float> difference(SIZE); subVector(x_reference, x, difference); // To measure the distance, we use the scalar product measure we have // introduced above. Note, that this operator wants the first vector in // accumulated format and the second one in distributed format. The solution // we got from the CG algorithm is accumulated (because CG is an inverse // operator). This means, we have to distribute the solution to have mixed // formats. agile::GPUVector<float> difference_dist(difference); com.distribute(difference_dist); std::cout << "L2 of difference: " << std::sqrt(std::abs(scalar_product(difference, difference_dist))) << std::endl; // So, that's it. return 0; }
int test_boost_point_arithmetic(point_type const& left, point_type const& right) { int error_count = 0; point_type a(left); point_type b = right; point_type sum(a); boost::geometry::add_point(sum, b); std::cout << "Point addition: a + b = " << to_string(sum) << "\n"; error_count += test_expected_value<0>(sum, left.template get<0>() + right.template get<0>()); error_count += test_expected_value<1>(sum, left.template get<1>() + right.template get<1>()); point_type difference(a); boost::geometry::subtract_point(difference, b); std::cout << "Point subtraction: a - b = " << to_string(difference) << "\n"; error_count += test_expected_value<0>(difference, left.template get<0>() - right.template get<0>()); error_count += test_expected_value<1>(difference, left.template get<1>() - right.template get<1>()); point_type pointwise_product(a); boost::geometry::multiply_point(pointwise_product, b); std::cout << "Pointwise product: " << to_string(pointwise_product) << "\n"; error_count += test_expected_value<0>(pointwise_product, left.template get<0>() * right.template get<0>()); error_count += test_expected_value<1>(pointwise_product, left.template get<1>() * right.template get<1>()); point_type pointwise_quotient(a); boost::geometry::divide_point(pointwise_quotient, b); std::cout << "Pointwise quotient: " << to_string(pointwise_quotient) << "\n"; error_count += test_expected_value<0>(pointwise_quotient, left.template get<0>() / right.template get<0>()); error_count += test_expected_value<1>(pointwise_quotient, left.template get<1>() / right.template get<1>()); point_type scalar_product(a); boost::geometry::multiply_value(scalar_product, 2); std::cout << "Scalar product: " << to_string(scalar_product) << "\n"; error_count += test_expected_value<0>(scalar_product, left.template get<0>() * 2); error_count += test_expected_value<1>(scalar_product, left.template get<1>() * 2); point_type scalar_quotient(a); boost::geometry::divide_value(scalar_quotient, 2); std::cout << "Scalar quotient: " << to_string(scalar_quotient) << "\n"; error_count += test_expected_value<0>(scalar_quotient, left.template get<0>() / 2); error_count += test_expected_value<1>(scalar_quotient, left.template get<1>() / 2); double how_far = distance(left, right); // double other_distance = left.distance_to(right); std::cout << "Geographic distance between points: " << how_far << "\n"; return error_count; }
/*ARGSUSED*/ EXPORT void get_state_1d_overlay( float *coords, Locstate s, COMP_TYPE *ct, HYPER_SURF *hs, INTERFACE *intfc, INIT_DATA *init, int stype) { ONED_OVERLAY *olay = (ONED_OVERLAY*)ct->extra; INPUT_SOLN **is = olay->is; RECT_GRID *gr = &is[0]->grid; INTERFACE *intfc1d = olay->intfc1d; float x, coords1d[3], m, sp; float dcoords[3]; float c0[3], c1[3]; float alpha; int i0[3], i1[3], i, dim = ct->params->dim; int nvars = olay->prt->n_restart_vars; static Locstate s0 = 0, s1 = 0; debug_print("init_states","Entered get_state_1d_overlay()\n"); if (s0 == NULL) { (*ct->params->_alloc_state)(&s0,ct->params->sizest); (*ct->params->_alloc_state)(&s1,ct->params->sizest); } for (i = 0; i < dim; ++i) dcoords[i] = coords[i] - olay->origin[i]; switch (olay->overlay_type) { case RADIAL_OVERLAY: x = mag_vector(dcoords,dim); break; case CYLINDRICAL_OVERLAY: sp = scalar_product(dcoords,olay->direction,dim); for (i = 0; i < dim; ++i) dcoords[i] -= sp*olay->direction[i]; x = mag_vector(dcoords,dim); break; case RECTANGULAR_OVERLAY: x = scalar_product(dcoords,olay->direction,dim); break; case OVERLAY_TYPE_UNSET: default: x = -HUGE_VAL; screen("ERROR in get_state_1d_overlay(), unset overlay type\n"); clean_up(ERROR); break; } if (x > gr->U[0]) x = gr->U[0]; if (x < gr->L[0]) x = gr->L[0]; coords1d[0] = x; if (rect_in_which(coords1d,i0,gr) == FUNCTION_FAILED) { screen("ERROR in get_state_1d_overlay(), rect_in_which() failed\n"); print_general_vector("dcoords = ",dcoords,dim,"\n"); (void) printf("overlay type = %d, x = %g\n",olay->overlay_type,x); (void) printf("rectangular grid gr\n"); print_rectangular_grid(gr); (void) printf("One dimensional interface\n"); print_interface(intfc1d); clean_up(ERROR); } c0[0] = cell_center(i0[0],0,gr); if (x < c0[0]) { i1[0] = i0[0]; i0[0]--; if (i0[0] < 0) i0[0] = 0; c1[0] = c0[0]; c0[0] = cell_center(i0[0],0,gr); } else { i1[0] = i0[0] + 1; if (i1[0] >= gr->gmax[0]) i1[0] = i0[0]; c1[0] = cell_center(i1[0],0,gr); } if (component(c0,intfc1d) != ct->comp) { set_oned_state_from_interface(c0,s0,ct,olay); } else { g_restart_initializer(i0,nvars,ct->comp,is,s0,init); Init_params(s0,ct->params); } if (component(c1,intfc1d) != ct->comp) { set_oned_state_from_interface(c1,s1,ct,olay); } else { g_restart_initializer(i1,nvars,ct->comp,is,s1,init); Init_params(s1,ct->params); } alpha = (c1[0] > c0[0]) ? (x - c0[0])/(c1[0] - c0[0]) : 0.5; ct->params->dim = 1; interpolate_states(olay->front,1.0-alpha,alpha,c0,s0,c1,s1,s); ct->params->dim = dim; m = Mom(s)[0]; switch (olay->overlay_type) { case RADIAL_OVERLAY: case CYLINDRICAL_OVERLAY: if (x > 0.0) { for (i = 0; i < dim; ++i) Mom(s)[i] = m*dcoords[i]/x; } else { for (i = 0; i < dim; ++i) Mom(s)[i] = 0.0; } break; case RECTANGULAR_OVERLAY: for (i = 0; i < dim; ++i) Mom(s)[i] = m*olay->direction[i]; break; case OVERLAY_TYPE_UNSET: default: screen("ERROR in get_state_1d_overlay(), unset overlay type\n"); clean_up(ERROR); break; } set_state(s,stype,s); } /*end get_state_1d_overlay*/
int waveform(double *H_p, double *H_x, double *x,double r, double costh) { FILE *data; double *Rxyz, *Vxyz, *Axyz; // Spatial location, velocity, and Axyzeleration (for waveform calculations) double vec2,Rxyz_dot_Axyz; // Some auxiliary quantities double **MQ; // Components of the Mass Quadrupole double **Kronecker; // Kronecker's delta double dpsidt, dchidt,dphidt, dtaudt; double drdt, dcosthdt; long unsigned j,k; // Counters for Cartesian coordinates /*----- Allocating memory for the Position, Velocity, and Acceleration of the Particle in BL associated Cartesian Coordinates -----*/ Rxyz = Realvector(1,4); //Cartesian coordinates (X_p,Y_p,Z_p) Vxyz = Realvector(1,4); // velocity wrt the Boyer-Lindquist Time T_p Axyz = Realvector(1,4); // Acceleration wrt the BL time T_p /*-- Computing analytical expressions for r(w_r) and cos(th)[w_th] and the radial and polar phases psi & chi --*/ analytic_r_costh(x, &r, &costh); /*----- Computing velocities d(Chi,Psi,Phi, dTau)/dt-----*/ dchi_dpsi_dphi_dt(x, r, costh, &dpsidt, &dchidt,&dphidt, &dtaudt); drdt = dRdt; dcosthdt= dCOSTHdt; flat_space_XVA(x,r,costh,dphidt, drdt, dcosthdt, Rxyz, Vxyz, Axyz); /*----- Scalar and vectorial products -----*/ scalar_product(&vec2,Vxyz,Vxyz); scalar_product(&Rxyz_dot_Axyz,Rxyz,Axyz); /*----- Computing the Mass Quadrupole -----*/ //----- Initializing the Kronecker's delta -----// Kronecker = Realmatrix(1, 3, 1, 3); for(j=1;j<=3;j++) { for(k=1;k<=3;k++) { if (k == j) Kronecker[j][k] = 1.0; else Kronecker[j][k] = 0.0; } } MQ = Realmatrix(1, 3, 1, 3); for(j=1;j<=3;j++) { for(k=1;k<=3;k++) { if (j > k) MQ[j][k] = MQ[k][j]; // off-diagonal components else MQ[j][k] = MASS_SCO*( Rxyz[j]*Axyz[k] + Rxyz[k]*Axyz[j] + 2.0*Vxyz[j]*Vxyz[k]- 2.0*Kronecker[j][k]*(Rxyz_dot_Axyz + vec2)/3.0 ); } } /*-----------------------------------------------*/ /*----- Computing the Waveform Contribution -----*/ /*-----------------------------------------------*/ /*----- Initializing the Waveform Polarizations associated with this Observer -----*/ *H_p = 0.0; *H_x = 0.0; /*----- Adding the Mass Quadrupole -----*/ for (j=1;j<=3;j++) { for (k=1;k<=3;k++) { *H_p += E_p[j][k]*MQ[j][k]; *H_x += E_x[j][k]*MQ[j][k]; // printf("%4,6e %4,6e %4,6e\n",E_p[j][k],E_x[j][k],MQ[j][k] ); } } /*----- Rescaling with the Distant from the Source to the Observer -----*/ *H_p /= DL; *H_x /= DL; // hLISA(n_sampling, t, h_I, h_II, H_p, H_x); free_Realmatrix(Kronecker, 1, 3, 1, 3); free_Realmatrix(MQ, 1, 3, 1, 3); free_Realvector(Rxyz, 1, 4); free_Realvector(Vxyz, 1, 4); free_Realvector(Axyz, 1, 4); /*----- This is the end -----*/ return 0; }