// file: $isip/class/algo/Math/math_07.cc // version: $Id: math_07.cc 8037 2002-04-01 20:04:33Z gao $ // // isip include files // #include "Math.h" // method: computeFuncCalcEnumerateFloat // // arguments: // AlgorithmData& output: (output) output data // const Vector& input: (input) input data // // return: a bool8 value indicating status // // this method implements following function: // Y = operation(0) {a(0)*function(0)(X(0))} operation(1) // {a(1)*function(1)(X(1))} operation(2) {a(2)*function(2)(X(2))}... // ... + const, // where X(0), X(1), ... are the parts of input VectorFloat splitted // by number of operands. These can be VectorFloat. If // user wants to implement this for scalar or single vector, then // number of operands will be 1. For scalar input the length of the // input vector will be 1. // bool8 Math::computeFuncCalcEnumerateFloat(AlgorithmData& output_a, const Vector& input_a) { // check algorithm and implementation names // if ((algorithm_d != FUNCTION_CALCULATOR) || (implementation_d != ENUMERATE)) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } // compute the number of combinations // if (num_operands_d != input_a.length()) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } // input should be either vector or matrix // for (int32 j = 0; j < num_operands_d; j++) { if ((input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) && (input_a(j).getDataType() != AlgorithmData::MATRIX_FLOAT)) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } // set the mode of output // if (input_a(0).getDataType() == AlgorithmData::VECTOR_FLOAT) { output_a.makeVectorFloat(); } else { output_a.makeMatrixFloat(); } // loop over the number of input operands // for (int32 j = 0; j < num_operands_d; j++) { // declare temporary input variable // AlgorithmData tmp_input; // set the type to either matrix or vector, depending upon the // type of the input // if (input_a(j).getDataType() == AlgorithmData::VECTOR_FLOAT) { tmp_input.makeVectorFloat().assign(input_a(j).getVectorFloat()); } else { tmp_input.makeMatrixFloat().assign(input_a(j).getMatrixFloat()); } // branch on function: // apply function and corresponding weight // if (function_d(j) == (int32)IDENTITY) { // do nothing // } // function: EXP // else if (function_d(j) == (int32)EXP) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().exp(); } // function: EXP2 // else if (function_d(j) == (int32)EXP2) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().exp2(); } // function: EXP10 // else if (function_d(j) == (int32)EXP10) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().exp10(); } // function: FACTORIAL // else if (function_d(j) == (int32)FACTORIAL) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().factorial(); } // function: LOG // else if (function_d(j) == (int32)LOG) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } // check vector elements // for (int32 index = tmp_input.getVectorFloat().length() - 1; index >= 0; index--) { if (tmp_input.getVectorFloat()(index) <= (float32)0.0 ) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } tmp_input.getVectorFloat().log(); } // function: LOG2 // else if (function_d(j) == (int32)LOG2) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } // check vector elements // for (int32 index = tmp_input.getVectorFloat().length() - 1; index >= 0; index--) { if (tmp_input.getVectorFloat()(index) <= (float32)0.0 ) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } tmp_input.getVectorFloat().log2(); } // function: LOG10 // else if (function_d(j) == (int32)LOG10) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } // check vector elements // for (int32 index = tmp_input.getVectorFloat().length() - 1; index >= 0; index--) { if (tmp_input.getVectorFloat()(index) <= (float32)0.0 ) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } tmp_input.getVectorFloat().log10(); } // function: LOG1P // else if (function_d(j) == (int32)LOG1P) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } // check vector elements // for (int32 index = tmp_input.getVectorFloat().length() - 1; index >= 0; index--) { if (tmp_input.getVectorFloat()(index) <= (float32)0.0 ) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } tmp_input.getVectorFloat().log1p(); } // function: ABS // else if (function_d(j) == (int32)ABS) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().abs(); } // function: NEG // else if (function_d(j) == (int32)NEG) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { tmp_input.getVectorFloat().neg(); } else if (input_a(j).getDataType() != AlgorithmData::MATRIX_FLOAT) { tmp_input.getMatrixFloat().neg(); } else { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } } // function: ROUND // else if (function_d(j) == (int32)ROUND) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().round(); } // function: CEIL // else if (function_d(j) == (int32)CEIL) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().ceil(); } // function: FLOOR // else if (function_d(j) == (int32)FLOOR) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().floor(); } // function: RFLOOR // else if (function_d(j) == (int32)RFLOOR) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().rfloor(); } // function: SIN // else if (function_d(j) == (int32)SIN) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().sin(); } // function: COS // else if (function_d(j) == (int32)COS) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().cos(); } // function: TAN // else if (function_d(j) == (int32)TAN) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().tan(); } // function: ASIN // else if (function_d(j) == (int32)ASIN) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().asin(); } // function: ACOS // else if (function_d(j) == (int32)ACOS) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().acos(); } // function: ATAN // else if (function_d(j) == (int32)ATAN) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().atan(); } // function: SQUARE // else if (function_d(j) == (int32)SQUARE) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().square(); } // function: SQRT // else if (function_d(j) == (int32)SQRT) { if (input_a(j).getDataType() != AlgorithmData::VECTOR_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getVectorFloat().sqrt(); } // function: INVERSE // else if (function_d(j) == (int32)INVERSE) { if (input_a(j).getDataType() != AlgorithmData::MATRIX_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getMatrixFloat().inverse(); } // function: TRANSPOSE // else if (function_d(j) == (int32)TRANSPOSE) { if (input_a(j).getDataType() != AlgorithmData::MATRIX_FLOAT) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_FNDTYP, __FILE__, __LINE__); } tmp_input.getMatrixFloat().transpose(); } // invalid function // else { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_UNKFUN, __FILE__, __LINE__); } // apply weight // if (input_a(j).getDataType() == AlgorithmData::VECTOR_FLOAT) { tmp_input.getVectorFloat().mult(weight_d(j)); } else if (input_a(j).getDataType() == AlgorithmData::MATRIX_FLOAT) { tmp_input.getMatrixFloat().mult(weight_d(j)); } else { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", Error::ARG, __FILE__, __LINE__); } // branch on operation: // Operation: ASSIGN // if (operation_d(j) == (int32)ASSIGN) { // this operation is possible between same data types // // tmp_output.getVectorFloat().assign(tmp_input.getVectorFloat()); if (output_a.getDataType() != input_a(j).getDataType()) { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_MATCH, __FILE__, __LINE__); } if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { output_a.makeVectorFloat().assign(tmp_input.getVectorFloat()); } else { output_a.makeMatrixFloat().assign(tmp_input.getMatrixFloat()); } } // Operation: ADD // else if (operation_d(j) == (int32)ADD) { computeAddFloat(output_a, tmp_input); } // Operation: SUBTRACT // else if (operation_d(j) == (int32)SUBTRACT) { computeSubFloat(output_a, tmp_input); } // Operation: MULTIPLY // else if (operation_d(j) == (int32)MULTIPLY) { computeMultFloat(output_a, tmp_input); } // Operation: DIVIDE // else if (operation_d(j) == (int32)DIVIDE) { computeDivFloat(output_a, tmp_input); } // invalid operation // else { return Error::handle(name(), L"computeFuncCalcEnumerateFloat", ERR_UNKOPE, __FILE__, __LINE__); } } // add the constant term // if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { output_a.getVectorFloat().add((float32)const_d); } else { output_a.getMatrixFloat().add((float32)const_d); } // exit gracefully // return true; } // method: computeAddFloat // // arguments: // AlgorithmData& output: (input/output) 1st operand and output // const AlgorithmData& input: (input) 2nd operand // // return: logical error status // bool8 Math::computeAddFloat(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 ilen = input_a.getVectorFloat().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); // if either input is of length 1, perform scalar addition // if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getVectorFloat().add(scalar); } else if (olen == 1) { float32 scalar = output_a.getVectorFloat()(0); return output_a.getVectorFloat().add(input_a.getVectorFloat(), scalar); } // if they are the same length, do vector addition // else if (ilen == olen) { return output_a.getVectorFloat().add(input_a.getVectorFloat()); } // else error // else { return Error::handle(name(), L"computeAddFloat", 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_FLOAT) { if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getMatrixFloat().add(scalar); } else { return Error::handle(name(), L"computeAddFloat", ERR_MATCH, __FILE__, __LINE__); } } } else if (input_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); if (olen == 1) { float32 scalar = output_a.getVectorFloat()(0); return output_a.makeMatrixFloat().add(input_a.getMatrixFloat(), scalar); } else { return Error::handle(name(), L"computeAddFloat", ERR_MATCH, __FILE__, __LINE__); } } // if both are matrices, just add them // else if (output_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { return output_a.getMatrixFloat().add(input_a.getMatrixFloat()); } } // exit gracefully // return true; } // method: computeSubFloat // // arguments: // AlgorithmData& output: (input/output) 1st operand and output // const AlgorithmData& input: (input) 2nd operand // // return: logical error status // bool8 Math::computeSubFloat(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 ilen = input_a.getVectorFloat().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); // if either input is of length 1, perform scalar subtraction // if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getVectorFloat().sub(scalar); } else if (olen == 1) { // note this is different from addition since the operation is // not cummative // float32 scalar = output_a.getVectorFloat()(0); output_a.getVectorFloat().setLength(ilen); output_a.getVectorFloat().assign(scalar); return output_a.getVectorFloat().sub(input_a.getVectorFloat()); } // if they are the same length, do vector subtraction // else if (ilen == olen) { return output_a.getVectorFloat().sub(input_a.getVectorFloat()); } // else error // else { return Error::handle(name(), L"computeSubFloat", 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_FLOAT) { if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getMatrixFloat().sub(scalar); } else { return Error::handle(name(), L"computeSubFloat", ERR_MATCH, __FILE__, __LINE__); } } } else if (input_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { // 1 vector, 1 matrix. if the vector is of length 1, treat it as a // scalar // if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); if (olen == 1) { // note this is different from addition since the operation is // not cummative // float32 scalar = output_a.getVectorFloat()(0); output_a.makeMatrixFloat().setDimensions(input_a.getMatrixFloat()); output_a.getMatrixFloat().assign(scalar); return output_a.getMatrixFloat().sub(input_a.getMatrixFloat()); } else { return Error::handle(name(), L"computeSubFloat", ERR_MATCH, __FILE__, __LINE__); } } // if both are matrices, just subtract them // else if (output_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { return output_a.getMatrixFloat().sub(input_a.getMatrixFloat()); } } // exit gracefully // return true; } // method: computeMultFloat // // arguments: // AlgorithmData& output: (input/output) 1st operand and output // const AlgorithmData& input: (input) 2nd operand // // return: logical error status // bool8 Math::computeMultFloat(AlgorithmData& output_a, const AlgorithmData& input_a) const { if (input_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 ilen = input_a.getVectorFloat().length(); if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); // if either input is of length 1, perform scalar multiplication // if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getVectorFloat().mult(scalar); } else if (olen == 1) { float32 scalar = output_a.getVectorFloat()(0); return output_a.getVectorFloat().mult(input_a.getVectorFloat(), scalar); } // if they are the same length, do vector multiplication // else if (ilen == olen) { return output_a.getVectorFloat().mult(input_a.getVectorFloat()); } // else error // else { return Error::handle(name(), L"computeMultFloat", ERR_MATCH, __FILE__, __LINE__); } } else if (output_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { // if the vector is length 1, do scalar multiplication // if (ilen == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getMatrixFloat().mult(scalar); } // if the vector is the correct length for multv, do it // else if (ilen == output_a.getMatrixFloat().getNumColumns()) { VectorFloat tmp_out; output_a.getMatrixFloat().multv(tmp_out, input_a.getVectorFloat()); output_a.makeVectorFloat().assign(tmp_out); } // else error // else { return Error::handle(name(), L"computeMultFloat", ERR_MATCH, __FILE__, __LINE__); } } } // input is a matrix // else if (input_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { if (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT) { int32 olen = output_a.getVectorFloat().length(); // if the vector is of length 1, do scalar multiplication // if (olen == 1) { float32 scalar = output_a.getVectorFloat()(0); output_a.makeMatrixFloat().assign(input_a.getMatrixFloat()); return output_a.getMatrixFloat().mult(scalar); } // if the vector is the correct length for vmult, do it // else if (olen == input_a.getMatrixFloat().getNumRows()) { VectorFloat tmp_out; input_a.getMatrixFloat().vmult(tmp_out, output_a.getVectorFloat()); output_a.getVectorFloat().assign(tmp_out); } // else error // else { return Error::handle(name(), L"computeMultFloat", ERR_MATCH, __FILE__, __LINE__); } } else if (output_a.getDataType() == AlgorithmData::MATRIX_FLOAT) { return output_a.getMatrixFloat().mult(input_a.getMatrixFloat()); } } // exit gracefully // return true; } // method: computeDivFloat // // arguments: // AlgorithmData& output: (input/output) 1st operand and output // const AlgorithmData& input: (input) 2nd operand // // return: logical error status // // this method compute value for float32 type // bool8 Math::computeDivFloat(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_FLOAT) && (output_a.getDataType() == AlgorithmData::MATRIX_FLOAT)) { if (input_a.getVectorFloat().length() > 1) { return Error::handle(name(), L"computeDiv_Float", ERR_MATCH, __FILE__, __LINE__); } VectorFloat vec(input_a.getVectorFloat()); float32 scalar = vec(0); output_a.getMatrixFloat().div(scalar); } // both inputs are vectors // else if ((input_a.getDataType() == AlgorithmData::VECTOR_FLOAT) && (output_a.getDataType() == AlgorithmData::VECTOR_FLOAT)) { // if the input is of length 1, do scalar division // if (input_a.getVectorFloat().length() == 1) { float32 scalar = input_a.getVectorFloat()(0); return output_a.getVectorFloat().div(scalar); } // if the lengths are equal, do element-wise division // else if (input_a.getVectorFloat().length() == output_a.getVectorFloat().length()) { return output_a.getVectorFloat().div(input_a.getVectorFloat()); } else { return Error::handle(name(), L"computeDivFloat", ERR_MATCH, __FILE__, __LINE__); } } else { return Error::handle(name(), L"computeDivFloat", ERR_MATCH, __FILE__, __LINE__); } // exit gracefully // return true; }