RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
UnaryFunction.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.units;
6
7import java.util.Objects;
8
16@FunctionalInterface
17public interface UnaryFunction {
20
27 double apply(double input);
28
44 Objects.requireNonNull(next, "The next operation in the chain must be provided");
45
46 return x -> next.apply(this.apply(x));
47 }
48
55 default UnaryFunction mult(UnaryFunction multiplier) {
56 Objects.requireNonNull(multiplier, "A multiplier function must be provided");
57
58 return x -> this.apply(x) * multiplier.apply(x);
59 }
60
67 default UnaryFunction mult(double multiplier) {
68 return x -> this.apply(x) * multiplier;
69 }
70
77 default UnaryFunction div(UnaryFunction divisor) {
78 Objects.requireNonNull(divisor, "A divisor function must be provided");
79
80 return x -> {
81 double numerator = this.apply(x);
82
83 // fast-track to avoid another function call
84 // avoids returning NaN if divisor is also zero
85 if (numerator == 0) {
86 return 0;
87 }
88
89 double div = divisor.apply(x);
90 return numerator / div; // NOTE: returns +Infinity or -Infinity if div is zero
91 };
92 }
93
100 default UnaryFunction div(double divisor) {
101 return x -> this.apply(x) / divisor;
102 }
103
110 default UnaryFunction exp(UnaryFunction exponent) {
111 Objects.requireNonNull(exponent, "An exponent function must be provided");
112
113 return x -> Math.pow(this.apply(x), exponent.apply(x));
114 }
115
122 default UnaryFunction exp(double exponent) {
123 return x -> Math.pow(this.apply(x), exponent);
124 }
125}
default UnaryFunction exp(UnaryFunction exponent)
default UnaryFunction exp(double exponent)
default UnaryFunction pipeTo(UnaryFunction next)
default UnaryFunction div(double divisor)
default UnaryFunction mult(double multiplier)
default UnaryFunction mult(UnaryFunction multiplier)
double apply(double input)
default UnaryFunction div(UnaryFunction divisor)