double* FunctionAST::get_scaling_coeff(double* scaling_coeff, double* wavelet_coeff, int n, int l) { int i, k; double* d; double* s; double is_odd; k = this->func->k; d = Vector_initialize(NULL, 0, 2 * k); for (i=0; i<k; i++) { d[i] = scaling_coeff[i]; d[i+k] = wavelet_coeff[i]; } is_odd = l - 2*(l/2); // apply the two scale relationship to get difference coeff // in 1d this is O(k^2) floops (in 3d this is O(k^4) flops) if (!is_odd) { s = Multiply_Vector_Matrix(d, k * 2, this->func->hg0, k * 2, k); } else { s = Multiply_Vector_Matrix(d, k * 2, this->func->hg1, k * 2, k); } return s; }
double* FunctionAST::diff_coefficients(double* left, double* center, double* right, int n) { int i, k; double* resultVector; double* tempRpLeft; double* tempR0Center; double* tempRmRight; k = this->func->k; resultVector = Vector_initialize(NULL, 0, k+ADD_K); tempRpLeft = Multiply_Matrix_Vector(this->func->rp, k+ADD_K, k+ADD_K, left, k+ADD_K); tempR0Center = Multiply_Matrix_Vector(this->func->r0, k+ADD_K, k+ADD_K, center, k+ADD_K); tempRmRight = Multiply_Matrix_Vector(this->func->rm, k+ADD_K, k+ADD_K, right, k+ADD_K); for (i=0; i<k+ADD_K; i++) { resultVector[i] = tempRpLeft[i] + tempR0Center[i] + tempRmRight[i]; } free(tempRpLeft); free(tempR0Center); free(tempRmRight); Vector_scale(resultVector, this->func->k+ADD_K, pow(2.0, n)); return resultVector; }
AST* FunctionAST::non_ready_operands(AST* evaluated_operands_list, AST* ready_operand_indices, int n, int l) { int i; int j, i1, i2; int le; int lr; double* coeff; double* result_coeff; AST* result_node; AST* target_node; AST* temp; le = ast_len(evaluated_operands_list); lr = ast_len(ready_operand_indices); result_coeff = Vector_initialize(0, 0, this->k); int lenIndices = ast_len(ready_operand_indices); for (j=0; j<lenIndices; j++) { evaluated_operands_list = ast_delete_item(evaluated_operands_list, ast_get_item(ready_operand_indices, j)->intVal); } // not implemented yet result_node = ast_create_item(0, this->create_node(NULL, result_coeff, n, l, 1), NULL); // if all the operands are available for computing // then the operation is complete. Just replace the // operation node wiht the result node if (le == lr) { if (n == this->max_level) result_node->nodeVal->is_ready = 1; printf ("(1)\n"); return result_node; } // the operation is not complete yet. Some operands // are still at internal nodes else { AST* op = ast_create_item(NON_OPERATOR, NULL, NULL); printf ("===============\n"); ast_print_tree(op, 0, 0); printf ("\n"); ast_print_tree(evaluated_operands_list, 0, 0); printf ("\n"); ast_print_tree(result_node, 0, 0); printf ("\n"); evaluated_operands_list = ast_put_item(op, evaluated_operands_list); evaluated_operands_list = ast_insert_item(result_node, evaluated_operands_list); ast_print_tree(evaluated_operands_list, 0, 0); printf ("\n"); printf ("(2)\n"); return evaluated_operands_list; } }
double* FunctionAST::add_coefficients(double* coeff1, double* coeff2) { double* temp; temp = Vector_initialize(coeff1, 0, this->func->k); Vector_gaxpy(temp, 1.0, coeff2, 1.0, this->func->k); return temp; }
double* FunctionAST::inner_coefficients(double* coeff1, double* coeff2, double* inheritedResult) { #ifdef DEBUG printf ("[%s]\n", __func__); #endif double* result; if (coeff1 == NULL || coeff2 == NULL) { return Vector_initialize(NULL, 0, this->func->k+ADD_K); printf("this should never happen"); } result = Vector_initialize(NULL, 0, this->func->k+ADD_K); result[0] = Vector_inner(coeff1, coeff2, this->func->k+ADD_K); *inheritedResult += result[0]; this->func->tempInner += result[0]; //printf ("%12.8f:%12.8f\n", result[0], *inheritedResult); return result; }
// for co-ordinates at box nn, ll, evaluates the value using // coefficients at box n, l // this function assumes that x is Vector. double* FunctionAST::evaluate_at_box(int k, double* coeff, int n, int l, int nn, int ll, double* x) { int i; double* p; double coordinate; double temp; double* returnVector; returnVector = Vector_initialize(NULL, 0, this->func->k+ADD_K); for (i=0; i<this->func->k; i++) { coordinate = ((x[i] + ll) * (pow(2.0, n - nn))) - l; p = Vector_initialize(phi(coordinate, this->func->k), 0, this->func->k); temp = Vector_inner(coeff, p, this->func->k); returnVector[i] = temp * sqrt(pow(2.0, n)); free(p); } return returnVector; }
Vector Vector_scalar_add(const Vector vec, const double c) { return Vector_initialize(c + vec.x, c + vec.y); }
Vector Vector_scalar_mult(const Vector vec, const double c) { return Vector_initialize(c * vec.x, c * vec.y); }
Vector Vector_negate(const Vector vec) { return Vector_initialize(-vec.x, -vec.y); }
Vector Vector_mult(const Vector vec0, const Vector vec1) { return Vector_initialize(vec0.x * vec1.x, vec0.y * vec1.y); }
Vector Vector_sub(const Vector vec0, const Vector vec1) { return Vector_initialize(vec0.x - vec1.x, vec0.y - vec1.y); }
Vector Vector_add(const Vector vec0, const Vector vec1) { return Vector_initialize(vec0.x + vec1.x, vec0.y + vec1.y); }
Vector Vector_zero() { return Vector_initialize(0.0, 0.0); }
VectorPointer Vector_create(const double x, const double y) { VectorPointer rtn = (VectorPointer) malloc(sizeof(Vector)); *rtn = Vector_initialize(x, y); return rtn; }
static ParseResult parse_vertex_data( json_value *vertex, int *o_id, VectorPointer o_pos, char *o_label, int *o_fixed ) { ParseResult result; unsigned int fields = vertex->u.object.length; if (fields != 3) { result.status = PS_VALUE_LENGTH_ERR; result.emsg = "Each vertex specified in the input file needs to have exactly 3 fields"; return result; } char *fst_fieldname, *snd_fieldname, *thrd_fieldname; json_value *ident, *position, *label; fst_fieldname = vertex->u.object.values[0].name; snd_fieldname = vertex->u.object.values[1].name; thrd_fieldname = vertex->u.object.values[2].name; ident = vertex->u.object.values[0].value; position = vertex->u.object.values[1].value; label = vertex->u.object.values[2].value; if (strcmp(fst_fieldname, "id")) { result.status = PS_NAME_SPELL_ERR; result.emsg = "First field of a vertex needs to be named 'id'"; return result; } else if (strcmp(snd_fieldname, "fixed")) { result.status = PS_NAME_SPELL_ERR; result.emsg = "Second field of a vertex needs to be named 'fixed'"; return result; } else if (strcmp(thrd_fieldname, "label")) { result.status = PS_NAME_SPELL_ERR; result.emsg = "Third field of a vertex needs to be named 'label'"; return result; } int id, fixed; id = -99; if (ident->type == json_integer) { id = ident->u.integer; } else { result.status = PS_VALUE_TYPE_ERROR; result.emsg = "Field id does not have an integer as value"; return result; } *o_id = id; fixed = 0; Vector pos; pos = Vector_zero(); if (position->type == json_array) { fixed = 1; int length; length = position->u.array.length; if (length != 2) { result.status = PS_VALUE_LENGTH_ERR; result.emsg = "Position coordinates != 2"; return result; } json_value *j_x, *j_y; j_x = position->u.array.values[0]; j_y = position->u.array.values[1]; double fixed_x, fixed_y; fixed_x = fixed_y = 0; if (j_x->type == json_double) { if (Util_in_range(0, 1, (double) j_x->u.dbl)) { fixed_x = (double) j_x->u.dbl; } else { result.status = PS_VALUE_RANGE_ERROR; result.emsg = "X-coordinate must lie in range (0, 1)"; return result; } } else { result.status = PS_VALUE_TYPE_ERROR; result.emsg = "X-coordinate be of type double"; return result; } if (j_y->type == json_double) { if (Util_in_range(0, 1, (double) j_y->u.dbl)) { fixed_y = (double) j_y->u.dbl; } else { result.status = PS_VALUE_RANGE_ERROR; result.emsg = "Y-coordinate must lie in range (0, 1)"; return result; } } else { result.status = PS_VALUE_TYPE_ERROR; result.emsg = "Y-coordinate be of type double"; return result; } pos = Vector_initialize(fixed_x, fixed_y); } else if (position->type != json_null) { result.status = PS_VALUE_TYPE_ERROR; result.emsg = "The value fixed must be an array with 2 elements or null"; return result; } *o_pos = pos; *o_fixed = fixed; char vertex_label[MAX_LABEL_LENGTH]; if (label->type == json_string) { strcpy(vertex_label, label->u.string.ptr); } else if (label->type == json_null) { vertex_label[0] = 0; } else { result.status = PS_VALUE_TYPE_ERROR; result.emsg = "Label must be of type string"; return result; } strcpy(o_label, vertex_label); result.status = PS_SUCCESS; result.emsg = NULL; return result; }