Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving a maths function class for speed c++
    primarykey
    data
    text
    <p>I wrote some maths functions for use within my program, they are going to get very heavy use. I would like to offer the code up to see if a) there are logic improvements to made and b) if there is a better way of doing any of this. Its a header file which is included where needed. </p> <p>I'm not compiling for c++11 so please bare that in mind. - I'm also aware that the rootDouble for negative numbers is not mathematically correct. </p> <p>I think the first thing which may come up is converting the vector inputs to pass by reference, comments around that are welcome.</p> <p>In terms of me accepting an answer, i'd like to know what and how these functions could be improved for speed.</p> <p>++ I've posted this rather quickly, hope I didn't leave any embarrassing errors inside!</p> <pre><code>#ifndef MATHSFUNCTIONS_H_ #define MATHSFUNCTIONS_H_ #include &lt;algorithm&gt; #include &lt;vector&gt; #include &lt;numeric&gt; #include &lt;cmath&gt; class MathsFunctions { public: MathsFunctions(); virtual ~MathsFunctions(); inline static double squareDouble(double input) { return input * input; } inline static double rootDouble(double input) { if (input == 0.0) { return 0.0; } else if ( input &lt; 0.0) { input = flipDouble(input); input = sqrt(input); return flipDouble(input); } return sqrt(input); } inline static double flipDouble(double input) { return input * -1; } inline static double rangeInVec(std::vector&lt;double&gt; inputs) { return maxInVec(inputs) - minInVec(inputs); } inline static double stdDevInVec(std::vector&lt;double&gt; inputs) { if (inputs.size() &lt; 2) {return 0.0;} double mean = meanInVec(inputs); double sq_sum = std::inner_product(inputs.begin(), inputs.end(), inputs.begin(), 0.0); return std::sqrt(sq_sum / inputs.size() - mean * mean); } inline static double meanInVec(std::vector&lt;double&gt; inputs) { double sum = std::accumulate(inputs.begin(), inputs.end(), 0.0); return sum / inputs.size(); } inline static double sumOfVec(std::vector&lt;double&gt; inputs) { double total = 0.0; for (unsigned int var = 0; var &lt; inputs.size(); ++var) { total += inputs[var]; } return total; } inline static double maxInVec(std::vector&lt;double&gt; inputs) { bool first = true; double max; for (unsigned int var = 0; var &lt; inputs.size(); ++var) { if (first) { max = inputs[var]; first = false; } else { if (inputs[var] &gt; max) { max = inputs[var]; } } } return max; } inline static double minInVec(std::vector&lt;double&gt; inputs) { bool first = true; double min; for (unsigned int var = 0; var &lt; inputs.size(); ++var) { if (first) { min = inputs[var]; first = false; } else { if (inputs[var] &lt; min) { min = inputs[var]; } } } return min; } inline static std::vector&lt;double&gt; weightValueVector(std::vector&lt;double&gt; inputs,std::vector&lt;double&gt; weights) { std::vector&lt;double&gt; results; for (unsigned x = 0; x &lt; inputs.size(); ++x) { results.push_back(inputs[x] * weights[x]); } return results; } }; #endif /* MATHSFUNCTIONS_H_ */ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload