Vector<Type>& Vector<Type>::operator-=(const Vector<Type>& other) { if (this->size() != other.size()) errorV("- Length error"); for(int i = 0; i < values.size(); i++) { values[i] -= other.values[i]; } return *this; }
//Variables Value* VariableExprAST::codeGen() const { // Look this variable up in the function. Value *v = ExprAST::namedValues[Name]; std::cout << "VariableExprAST: " << Name << " with type: " << v->getType()->getTypeID() << std::endl; return v ? v : errorV(std::string("Unknown variable name: ").append(Name).c_str()); }
Type operator*(Vector<Type> v1, Vector<Type> v2) { if (v1.size() != v2.size()) errorV("* Length error"); Type sum = 0; for(int i = 0; i < v1.size(); i++) { sum += v1(i) *= v2(i); } return sum; };
Vector<Type>& Vector<Type>::operator%=(const Vector<Type>& other) { if (this->size() != 3 && other.size() != 3) errorV("% length error"); Type v1 = values[1] * other.values[2] - values[2] * other.values[1]; Type v2 = values[2] * other.values[0] - values[0] * other.values[2]; Type v3 = values[0] * other.values[1] - values[1] * other.values[0]; values[0] = v1; values[1] = v2; values[2] = v3; return *this; }
//Function calls llvm::Value* CallExprAST::codeGen() const { // Look up the name in the global module table llvm::Function* calleeFunc = module->getFunction(callee); if(!calleeFunc) return errorV("Unkown function referenced"); // If argument mismatch error. if(calleeFunc->arg_size() != args.size()) return errorV("Incorrect number of arguments passed."); std::vector<Value*> argsVector; for(size_t i = 0; i < args.size(); ++i) { argsVector.push_back(args.at(i)->codeGen()); if(!argsVector.back()) return NULL; } return ExprAST::builder.CreateCall(calleeFunc, argsVector, "calltmp"); }
Vector<Type>& Vector<Type>::normalize() { double n = norm(); if(n == 0) errorV("norm = 0"); return (*this) /= n; }
double cosine(Vector<Type> v1, Vector<Type> v2) { double n1 = v1.norm(), n2 = v2.norm(); if (n1 == 0 || n2 == 0) errorV("norm = 0"); return v1 * v2 / (n1 * n2); }