Class LinearFunc
- All Implemented Interfaces:
Func
- Direct Known Subclasses:
LinearFunc.Updatable
Linear function:
f(x0, x1, ..., xn-1) =
b + a0x0 + a1x1
+...+ an-1xn-1.
Note: if b==+0.0 or b==−0.0, this sum is calculated as
The get(double...)
method of the instance of this class requires at least n arguments
and throws IndexOutOfBoundsException if the number of arguments is less.
Please note: if all ai coefficients are equal (averaging function),
this class does not spend Java memory for storing them.
So you can freely create averaging linear function with very large number of coefficients;
but in this case you should avoid calling a()
method.
This class is immutable and thread-safe: there are no ways to modify settings of the created instance.
- Author:
- Daniel Alievsky
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Updatable extension
of thelinear function
with one argument. -
Field Summary
Fields inherited from interface net.algart.math.functions.Func
ABS, ABS_DIFF, HALF_X_MINUS_Y, HALF_X_PLUS_Y, HALF_Y_MINUS_X, IDENTITY, MAX, MIN, POSITIVE_DIFF, REVERSE, SELECT, SELECT_FROM_8_DIRECTIONS_2D, SELECT_IF_GREATER, SELECT_IF_GREATER_OR_EQUAL, SHIFTS_ALONG_8_DIRECTIONS_2D, UPDATABLE_IDENTITY, X_MINUS_Y, X_PLUS_Y, Y_MINUS_X
-
Method Summary
Modifier and TypeMethodDescriptiondouble[]
a()
Returns an array containing all ai coefficients of this linear function.double
a
(int i) Returns ai coefficient of this linear function.double
b()
Returns b coefficient of this linear function.abstract double
get()
Equivalent toget
(new double[0]).abstract double
get
(double x0) Equivalent toget
(new double[] {x0}).abstract double
get
(double... x) Returns the result of this function for the given arguments: f(x0, x1, ..., xx.length-1).abstract double
get
(double x0, double x1) Equivalent toget
(new double[] {x0, x1}).abstract double
get
(double x0, double x1, double x2) Equivalent toget
(new double[] {x0, x1, x2}).abstract double
get
(double x0, double x1, double x2, double x3) Equivalent toget
(new double[] {x0, x1, x2, x3}).static LinearFunc
getAveragingInstance
(int n) Equivalent togetNonweightedInstance(0.0, 1.0/n, n)
: the average from n numbers.static LinearFunc
getInstance
(double b, double... a) Returns an instance of this class, describing the linear function with specified coefficients: b + a0x0 + a1x1 +...+ an-1xn-1.static LinearFunc
getInstance
(Range destRange, Range srcRange) Returns an instance of this class describing the following linear function with one argument: dmin + (dmax-dmin) * (x-smin) / (smax-smin), where smin..smax is srcRange and dmin..dmax is destRange.static LinearFunc
getNonweightedInstance
(double b, double a, int n) Returns an instance of this class, describing the linear function with the specified b and the specified number (n) of equal coefficients ai: b + a(x0 + x1 +...+ xn-1).static LinearFunc.Updatable
getUpdatableInstance
(double b, double a) Returns an instance of the updatable version of this class, describing the linear function with specified coefficients: b + ax0.boolean
Returns true ifn()
<=1 or if all ai coefficients are equal.int
n()
Returns the number of ai coefficients.toString()
Returns a brief string description of this object.
-
Method Details
-
getInstance
Returns an instance of this class, describing the linear function with specified coefficients: b + a0x0 + a1x1 +...+ an-1xn-1.The passed reference a is not maintained by the created instance: if necessary, the Java array is cloned.
- Parameters:
b
- the b coefficient.a
- the a coefficients.- Returns:
- the linear function with the given coefficients.
-
getNonweightedInstance
Returns an instance of this class, describing the linear function with the specified b and the specified number (n) of equal coefficients ai: b + a(x0 + x1 +...+ xn-1).- Parameters:
b
- the b coefficient.a
- the common value of all ai coefficients.n
- the number of ai coefficients.- Returns:
- the linear function with the given coefficients.
-
getAveragingInstance
Equivalent togetNonweightedInstance(0.0, 1.0/n, n)
: the average from n numbers.- Parameters:
n
- the number of ai coefficients.- Returns:
- the function calculating average from n numbers.
-
getInstance
Returns an instance of this class describing the following linear function with one argument: dmin + (dmax-dmin) * (x-smin) / (smax-smin), where smin..smax is srcRange and dmin..dmax is destRange. This function maps the source range srcRange to the destination range destRange, excepting the only case when srcRange.size()
==0. In that special case the behavior of the returned function is not specified (but no exceptions are thrown).- Parameters:
destRange
- the destination range.srcRange
- the source range.- Returns:
- the linear function mapping the source range to the destination range.
- Throws:
NullPointerException
- if one of the arguments is null.
-
getUpdatableInstance
Returns an instance of the updatable version of this class, describing the linear function with specified coefficients: b + ax0. Theset
method of this instance sets x[0]=(newResult-b)*aInv, where aInv=1.0/a is calculated while the instance creation.- Parameters:
b
- the b coefficient.a
- the a coefficient.- Returns:
- the updatable linear function with the given coefficients.
-
get
public abstract double get(double... x) Description copied from interface:Func
Returns the result of this function for the given arguments: f(x0, x1, ..., xx.length-1).This method must not change the values of x elements!
-
get
public abstract double get()Description copied from interface:Func
Equivalent toget
(new double[0]). Provides better performance because it does not require Java array creation. -
get
public abstract double get(double x0) Description copied from interface:Func
Equivalent toget
(new double[] {x0}). Provides better performance because it does not require Java array creation. -
get
public abstract double get(double x0, double x1) Description copied from interface:Func
Equivalent toget
(new double[] {x0, x1}). Provides better performance because it does not require Java array creation. -
get
public abstract double get(double x0, double x1, double x2) Description copied from interface:Func
Equivalent toget
(new double[] {x0, x1, x2}). Provides better performance because it does not require Java array creation. -
get
public abstract double get(double x0, double x1, double x2, double x3) Description copied from interface:Func
Equivalent toget
(new double[] {x0, x1, x2, x3}). Provides better performance because it does not require Java array creation. -
n
public int n()Returns the number of ai coefficients.- Returns:
- the number of argument of this function.
-
b
public double b()Returns b coefficient of this linear function.- Returns:
- b coefficient.
-
a
public double a(int i) Returns ai coefficient of this linear function.- Parameters:
i
- the index of the coefficient.- Returns:
- ai coefficient.
- Throws:
IndexOutOfBoundsException
- if the given index is negative or >=n()
-
a
public double[] a()Returns an array containing all ai coefficients of this linear function.If
isNonweighted()
method returns true, it can be more efficient, to save memory, not to use this method, but to get the common value of all coefficients viaa(0)
call (please not forget to check thatn()
>0).The returned array is never a reference to an internal array stored in this object: if necessary, the internal Java array is cloned.
- Returns:
- all ai coefficients.
-
isNonweighted
public boolean isNonweighted()Returns true ifn()
<=1 or if all ai coefficients are equal. This function works little faster in this case, because it can be simplified as b + a0(x0 + x1 +...+ xn-1).- Returns:
- true if
n()
<=1 or if all ai coefficients are equal.
-
toString
Returns a brief string description of this object.
-