📄 math_08.cc
字号:
// else if (operation_d(j) == (long)ADD) { computeAddDouble(output_a, tmp_input); } // Operation: SUBTRACT // else if (operation_d(j) == (long)SUBTRACT) { computeSubDouble(output_a, tmp_input); } // Operation: MULTIPLY // else if (operation_d(j) == (long)MULTIPLY) { computeMultDouble(output_a, tmp_input); } // Operation: DIVIDE // else if (operation_d(j) == (long)DIVIDE) { computeDivDouble(output_a, tmp_input); } // invalid operation // else { return Error::handle(name(), L"computeFuncCalcEnumerateDouble", ERR_UNKOPE, __FILE__, __LINE__); } } // add the constant term // if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { output_a.getVectorDouble().add((double)const_d); } else { output_a.getMatrixDouble().add((double)const_d); } // exit gracefully // return true;}// method: computeAddDouble//// arguments:// AlgorithmData& output: (input/output) 1st operand and output// const AlgorithmData& input: (input) 2nd operand//// return: logical error status//boolean Math::computeAddDouble(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long ilen = input_a.getVectorDouble().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); // if either input is of length 1, perform scalar addition // if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getVectorDouble().add(scalar); } else if (olen == 1) { double scalar = output_a.getVectorDouble()(0); return output_a.getVectorDouble().add(input_a.getVectorDouble(), scalar); } // if they are the same length, do vector addition // else if (ilen == olen) { return output_a.getVectorDouble().add(input_a.getVectorDouble()); } // else error // else { return Error::handle(name(), L"computeAddDouble", ERR_MATCH, __FILE__, __LINE__); } } // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getMatrixDouble().add(scalar); } else { return Error::handle(name(), L"computeAddDouble", ERR_MATCH, __FILE__, __LINE__); } } } else if (input_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); if (olen == 1) { double scalar = output_a.getVectorDouble()(0); return output_a.makeMatrixDouble().add(input_a.getMatrixDouble(), scalar); } else { return Error::handle(name(), L"computeAddDouble", ERR_MATCH, __FILE__, __LINE__); } } // if both are matrices, just add them // else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { return output_a.getMatrixDouble().add(input_a.getMatrixDouble()); } } // exit gracefully // return true;}// method: computeSubDouble//// arguments:// AlgorithmData& output: (input/output) 1st operand and output// const AlgorithmData& input: (input) 2nd operand//// return: logical error status//boolean Math::computeSubDouble(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long ilen = input_a.getVectorDouble().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); // if either input is of length 1, perform scalar subtraction // if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getVectorDouble().sub(scalar); } else if (olen == 1) { // note this is different from addition since the operation is // not cummative // double scalar = output_a.getVectorDouble()(0); output_a.getVectorDouble().setLength(ilen); output_a.getVectorDouble().assign(scalar); return output_a.getVectorDouble().sub(input_a.getVectorDouble()); } // if they are the same length, do vector subtraction // else if (ilen == olen) { return output_a.getVectorDouble().sub(input_a.getVectorDouble()); } // else error // else { return Error::handle(name(), L"computeSubDouble", ERR_MATCH, __FILE__, __LINE__); } } // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getMatrixDouble().sub(scalar); } else { return Error::handle(name(), L"computeSubDouble", ERR_MATCH, __FILE__, __LINE__); } } } else if (input_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); if (olen == 1) { // note this is different from addition since the operation is // not cummative // double scalar = output_a.getVectorDouble()(0); output_a.makeMatrixDouble().setDimensions(input_a.getMatrixDouble()); output_a.getMatrixDouble().assign(scalar); return output_a.getMatrixDouble().sub(input_a.getMatrixDouble()); } else { return Error::handle(name(), L"computeSubDouble", ERR_MATCH, __FILE__, __LINE__); } } // if both are matrices, just subtract them // else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { return output_a.getMatrixDouble().sub(input_a.getMatrixDouble()); } } // exit gracefully // return true;}// method: computeMultDouble//// arguments:// AlgorithmData& output: (input/output) 1st operand and output// const AlgorithmData& input: (input) 2nd operand//// return: logical error status//boolean Math::computeMultDouble(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long ilen = input_a.getVectorDouble().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); // if either input is of length 1, perform scalar multiplication // if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getVectorDouble().mult(scalar); } else if (olen == 1) { double scalar = output_a.getVectorDouble()(0); return output_a.getVectorDouble().mult(input_a.getVectorDouble(), scalar); } // if they are the same length, do vector multiplication // else if (ilen == olen) { return output_a.getVectorDouble().mult(input_a.getVectorDouble()); } // else error // else { return Error::handle(name(), L"computeMultDouble", ERR_MATCH, __FILE__, __LINE__); } } else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { // if the vector is length 1, do scalar multiplication // if (ilen == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getMatrixDouble().mult(scalar); } // if the vector is the correct length for multv, do it // else if (ilen == output_a.getMatrixDouble().getNumColumns()) { VectorDouble tmp_out; output_a.getMatrixDouble().multv(tmp_out, input_a.getVectorDouble()); output_a.makeVectorDouble().assign(tmp_out); } // else error // else { return Error::handle(name(), L"computeMultDouble", ERR_MATCH, __FILE__, __LINE__); } } } // input is a matrix // else if (input_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { if (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) { long olen = output_a.getVectorDouble().length(); // if the vector is of length 1, do scalar multiplication // if (olen == 1) { double scalar = output_a.getVectorDouble()(0); output_a.makeMatrixDouble().assign(input_a.getMatrixDouble()); return output_a.getMatrixDouble().mult(scalar); } // if the vector is the correct length for vmult, do it // else if (olen == input_a.getMatrixDouble().getNumRows()) { VectorDouble tmp_out; input_a.getMatrixDouble().vmult(tmp_out, output_a.getVectorDouble()); output_a.getVectorDouble().assign(tmp_out); } // else error // else { return Error::handle(name(), L"computeMultDouble", ERR_MATCH, __FILE__, __LINE__); } } else if (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE) { return output_a.getMatrixDouble().mult(input_a.getMatrixDouble()); } } // exit gracefully // return true;}// method: computeDivDouble//// arguments:// AlgorithmData& output: (input/output) 1st operand and output// const AlgorithmData& input: (input) 2nd operand//// return: logical error status//boolean Math::computeDivDouble(AlgorithmData& output_a, const AlgorithmData& input_a) const { // vector-matrix division is possible only if length of the // vector is 1. no other matrix division is possible // if ((input_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) && (output_a.getDataType() == AlgorithmData::MATRIX_DOUBLE)) { if (input_a.getVectorDouble().length() > 1) { return Error::handle(name(), L"computeDivDouble", ERR_MATCH, __FILE__, __LINE__); } VectorDouble vec(input_a.getVectorDouble()); double scalar = vec(0); output_a.getMatrixDouble().div(scalar); } // both inputs are vectors // else if ((input_a.getDataType() == AlgorithmData::VECTOR_DOUBLE) && (output_a.getDataType() == AlgorithmData::VECTOR_DOUBLE)) { // if the input is of length 1, do scalar division // if (input_a.getVectorDouble().length() == 1) { double scalar = input_a.getVectorDouble()(0); return output_a.getVectorDouble().div(scalar); } // if the lengths are equal, do element-wise division // else if (input_a.getVectorDouble().length() == output_a.getVectorDouble().length()) { return output_a.getVectorDouble().div(input_a.getVectorDouble()); } else { return Error::handle(name(), L"computeDivDouble", ERR_MATCH, __FILE__, __LINE__); } } else { return Error::handle(name(), L"computeDivDouble", ERR_MATCH, __FILE__, __LINE__); } // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -