Value* Vector_dot(const Vector* vector1, const Vector* vector2, const Context* ctx) { unsigned count = vector1->vals->count; if(count != vector2->vals->count && vector2->vals->count != 1) { /* Both vectors must have the same number of values */ return ValErr(mathError("Vectors must have the same dimensions for dot product.")); } /* Store the total value of the dot product */ Value* accum = ValInt(0); unsigned i; for(i = 0; i < count; i++) { Value* val2; if(vector2->vals->count == 1) { val2 = vector2->vals->args[0]; } else { val2 = vector2->vals->args[i]; } /* accum += v1[i] * val2 */ TP(tp); accum = TP_EVAL(tp, ctx, "@@+@@*@@", accum, Value_copy(vector1->vals->args[i]), Value_copy(val2)); } return accum; }
static Value* eval_exp(const Context* ctx, const ArgList* arglist, bool internal) { if(arglist->count != 1) { return ValErr(builtinArgs("exp", 1, arglist->count)); } TP(tp); return TP_EVAL(tp, ctx, "e^@@", Value_copy(arglist->args[0])); }
Value* Vector_cross(const Vector* u, const Vector* v, const Context* ctx) { /* Down to one statement from almost 100 lines because of TP_EVAL :) */ /* Now up to two statements because MSVC doesn't support statement expressions :( */ TP(tp); return TP_EVAL(tp, ctx, "<@2@*@6@ - @3@*@5@," " @3@*@4@ - @1@*@6@," " @1@*@5@ - @2@*@4@>", Value_copy(u->vals->args[0]), Value_copy(u->vals->args[1]), Value_copy(u->vals->args[2]), Value_copy(v->vals->args[0]), Value_copy(v->vals->args[1]), Value_copy(v->vals->args[2])); }
Value* Vector_magnitude(const Vector* vec, const Context* ctx) { TP(tp); return TP_EVAL(tp, ctx, "sqrt(dot(@1v,@1v))", Vector_copy(vec)); }