num-traits/src
bors[bot] a49013e338 Merge #59
59: Added `MulAdd` and `MulAddAssign` traits r=cuviper a=regexident

Both `f32` and `f64` implement fused multiply-add, which computes `(self * a) + b` with only one rounding error. This produces a more accurate result with better performance than a separate multiplication operation followed by an add:

```rust
fn mul_add(self, a: f32, b: f32) -> f32[src]
```

It is however not possible to make use of this in a generic context by abstracting over a trait.

My concrete use-case is machine learning, [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent) to be specific,  
where the core operation of updating the gradient could make use of `mul_add` for both its `weights: Vector` as well as its `bias: f32`:

```rust
struct Perceptron {
  weights: Vector,
  bias: f32,
}

impl MulAdd<f32, Self> for Vector {
  // ...
}

impl Perceptron {
  fn learn(&mut self, example: Vector, expected: f32, learning_rate: f32) {
    let alpha = self.error(example, expected, learning_rate);
    self.weights = example.mul_add(alpha, self.weights);
    self.bias = self.bias.mul_add(alpha, self.bias)
  }
}
```

(The actual impl of `Vector` would be generic over its value type: `Vector<T>`, thus requiring the trait.)

Co-authored-by: Vincent Esche <regexident@gmail.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
2018-05-04 19:12:41 +00:00
..
ops Merge #59 2018-05-04 19:12:41 +00:00
bounds.rs Re-introduce the std feature 2018-01-31 15:42:55 -08:00
cast.rs Further simplify float-to-int range checks 2018-03-13 13:38:17 -07:00
float.rs Comment the Rust version for NAN.is_sign_* behavior 2018-02-28 11:43:55 -08:00
identities.rs Update outdated FIXME 2018-04-10 19:51:03 +02:00
int.rs Re-introduce the std feature 2018-01-31 15:42:55 -08:00
lib.rs Merge #59 2018-05-04 19:12:41 +00:00
macros.rs allow unused macros 2018-02-27 17:09:43 -08:00
pow.rs Ensure infalliability of conversions, avoid closures. 2018-02-27 14:06:46 -05:00
real.rs Use forwarding macros to implement Float and Real 2018-02-27 16:33:04 -08:00
sign.rs Use more FloatCore in src/sign.rs 2018-02-27 21:50:44 -08:00