1 module dunitconversion.linearfunction; 2 3 import std.math; 4 5 /** 6 * The LinearFunction class describes a linear function 7 * with a format of Y = k*X + b 8 */ 9 struct LinearFunction { 10 11 @disable this(); 12 13 /** 14 * Constructor 15 * Params: 16 * k = K factor value 17 * b = bias value 18 */ 19 this (double k, double b) { 20 this.k = k; 21 this.b = b; 22 } 23 24 /** 25 * Checks if linear function is valid, i.e. if k != 0 26 * Returns: `true` if function is valid, `false` otherwise 27 */ 28 bool isValid() const nothrow { 29 return !approxEqual(k, 0); 30 } 31 32 /** 33 * Reverses current linear function so X = k*Y + b 34 * Returns: an object of type QLinearFunction containing inversed function 35 * Details: This function doesn't perform validity check so applying it to 36 * invalid function will cause division by zero 37 */ 38 LinearFunction inversed() const { 39 if (!isValid()) 40 throw new Exception("Can't inverse non-valid function"); 41 return LinearFunction(1. / k, -b / k); 42 } 43 44 /** 45 * Function value 46 * Params: x = function argument 47 * Returns: value of a function with an argument `x` 48 */ 49 double y(double x) const { 50 return k * x + b; 51 } 52 53 double k; /// Function scale factor K 54 double b; /// Function bias b 55 }