/* Make a new GLQ structure and set all the parameters needed */ GLQ * glq_new(int order, double lower, double upper) { GLQ *glq; int rc; glq = (GLQ *)malloc(sizeof(GLQ)); if(glq == NULL) { return NULL; } glq->order = order; glq->nodes = (double *)malloc(sizeof(double)*order); if(glq->nodes == NULL) { free(glq); return NULL; } glq->nodes_unscaled = (double *)malloc(sizeof(double)*order); if(glq->nodes_unscaled == NULL) { free(glq); free(glq->nodes); return NULL; } glq->weights = (double *)malloc(sizeof(double)*order); if(glq->weights == NULL) { free(glq); free(glq->nodes); free(glq->nodes_unscaled); return NULL; } rc = glq_nodes(order, glq->nodes_unscaled); if(rc != 0 && rc != 3) { switch(rc) { case 1: log_error("glq_nodes invalid GLQ order %d. Should be >= 2.", order); break; case 2: log_error("glq_nodes NULL pointer for nodes"); break; default: log_error("glq_nodes unknown error code %g", rc); break; } glq_free(glq); return NULL; } else if(rc == 3) { log_warning("glq_nodes max iterations reached in root finder"); log_warning("nodes might not have desired accuracy %g", GLQ_MAXERROR); } rc = glq_weights(order, glq->nodes_unscaled, glq->weights); if(rc != 0) { switch(rc) { case 1: log_error("glq_weights invalid GLQ order %d. Should be >= 2.", order); break; case 2: log_error("glq_weights NULL pointer for nodes"); break; case 3: log_error("glq_weights NULL pointer for weights"); break; default: log_error("glq_weights unknown error code %d\n", rc); break; } glq_free(glq); return NULL; } if(glq_set_limits(lower, upper, glq) != 0) { glq_free(glq); return NULL; } return glq; }
static char * test_glq_nodes() { double prec = pow(10, -9), nodes[19]; int rc, i, order; /* Test order 2 */ order = 2; rc = glq_nodes(order, nodes); sprintf(msg, "(order %d) return code %d, expected 0", order, rc); mu_assert(rc == 0, msg); for(i = 0; i < order; i++) { sprintf(msg, "(order %d, node %d) expected %.15f got %.15f", order, i, o2roots[i], nodes[i]); mu_assert_almost_equals(nodes[i], o2roots[i], prec, msg); } /* Test order 3 */ order = 3; rc = glq_nodes(order, nodes); sprintf(msg, "(order %d) return code %d, expected 0", order, rc); mu_assert(rc == 0, msg); for(i = 0; i < order; i++) { sprintf(msg, "(order %d, node %d) expected %.15f got %.15f", order, i, o3roots[i], nodes[i]); mu_assert_almost_equals(nodes[i], o3roots[i], prec, msg); } /* Test order 4 */ order = 4; rc = glq_nodes(order, nodes); sprintf(msg, "(order %d) return code %d, expected 0", order, rc); mu_assert(rc == 0, msg); for(i = 0; i < order; i++) { sprintf(msg, "(order %d, node %d) expected %.15f got %.15f", order, i, o4roots[i], nodes[i]); mu_assert_almost_equals(nodes[i], o4roots[i], prec, msg); } /* Test order 5 */ order = 5; rc = glq_nodes(order, nodes); sprintf(msg, "(order %d) return code %d, expected 0", order, rc); mu_assert(rc == 0, msg); for(i = 0; i < order; i++) { sprintf(msg, "(order %d, node %d) expected %.15f got %.15f", order, i, o5roots[i], nodes[i]); mu_assert_almost_equals(nodes[i], o5roots[i], prec, msg); } /* Test order 19 */ order = 19; rc = glq_nodes(order, nodes); sprintf(msg, "(order %d) return code %d, expected 0", order, rc); mu_assert(rc == 0, msg); for(i = 0; i < order; i++) { sprintf(msg, "(order %d, node %d) expected %.15f got %.15f", order, i, o19roots[i], nodes[i]); mu_assert_almost_equals(nodes[i], o19roots[i], prec, msg); } return 0; }