
net.algart.math.functions.LogFunc Maven / Gradle / Ivy
Show all versions of algart Show documentation
/*
* The MIT License (MIT)
*
* Copyright (c) 2007-2024 Daniel Alievsky, AlgART Laboratory (http://algart.net)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.algart.math.functions;
/**
* Logarithm function:
* f(x0) =
* logb(x0)
* (b is the base of the logarithm).
* More precisely, the result of this function is
* Math.log(x[0])*(1.0/Math.log(b))
for the instance
* returned by {@link #getInstance(double) getInstance(b)} method
* or StrictMath.log(x[0])*(1.0/Math.log(b))
for the instance
* returned by {@link #getStrictInstance(double) getStrictInstance(b)} method.
* The {@link #get} method of the instance of this class requires at least 1 argument
* and throws IndexOutOfBoundsException
if the number of arguments is 0.
*
* This class is immutable and thread-safe:
* there are no ways to modify settings of the created instance.
*
* @author Daniel Alievsky
*/
public abstract class LogFunc implements Func {
private final double base;
private LogFunc(double base) {
this.base = base;
}
/**
* Returns an instance of this class for the given logarithm base b
* using Math.log
method.
*
* This method returns special optimized versions of this class for base=Math.E
* and base=10.0
.
*
* @param b the base of the logarithm.
* @return an instance of this class using Math.log
method.
*/
public static LogFunc getInstance(double b) {
if (b == Math.E) {
return new LogFunc(b) {
public double get(double... x) {
return Math.log(x[0]);
}
public double get(double x0) {
return Math.log(x0);
}
public double get(double x0, double x1) {
return Math.log(x0);
}
public double get(double x0, double x1, double x2) {
return Math.log(x0);
}
public double get(double x0, double x1, double x2, double x3) {
return Math.log(x0);
}
};
} else if (b == 10.0) {
return new LogFunc(b) {
public double get(double... x) {
return Math.log10(x[0]);
}
public double get(double x0) {
return Math.log10(x0);
}
public double get(double x0, double x1) {
return Math.log10(x0);
}
public double get(double x0, double x1, double x2) {
return Math.log10(x0);
}
public double get(double x0, double x1, double x2, double x3) {
return Math.log10(x0);
}
};
} else {
final double mult = 1.0 / Math.log(b);
return new LogFunc(b) {
public double get(double... x) {
return Math.log(x[0]) * mult;
}
public double get(double x0) {
return Math.log(x0) * mult;
}
public double get(double x0, double x1) {
return Math.log(x0) * mult;
}
public double get(double x0, double x1, double x2) {
return Math.log(x0) * mult;
}
public double get(double x0, double x1, double x2, double x3) {
return Math.log(x0) * mult;
}
};
}
}
/**
* Returns an instance of this class for the given logarithm base b
* using StrictMath.log
method.
*
*
This method returns special optimized versions of this class for base=Math.E
* and base=10.0
.
*
* @param b the base of the logarithm.
* @return an instance of this class using StrictMath.log
method.
*/
public static LogFunc getStrictInstance(double b) {
if (b == Math.E) {
return new LogFunc(b) {
public double get(double... x) {
return StrictMath.log(x[0]);
}
public double get(double x0) {
return StrictMath.log(x0);
}
public double get(double x0, double x1) {
return StrictMath.log(x0);
}
public double get(double x0, double x1, double x2) {
return StrictMath.log(x0);
}
public double get(double x0, double x1, double x2, double x3) {
return StrictMath.log(x0);
}
};
} else if (b == 10.0) {
return new LogFunc(b) {
public double get(double... x) {
return StrictMath.log10(x[0]);
}
public double get(double x0) {
return StrictMath.log10(x0);
}
public double get(double x0, double x1) {
return StrictMath.log10(x0);
}
public double get(double x0, double x1, double x2) {
return StrictMath.log10(x0);
}
public double get(double x0, double x1, double x2, double x3) {
return StrictMath.log10(x0);
}
};
} else {
final double mult = 1.0 / Math.log(b);
return new LogFunc(b) {
public double get(double... x) {
return StrictMath.log(x[0]) * mult;
}
public double get(double x0) {
return StrictMath.log(x0) * mult;
}
public double get(double x0, double x1) {
return StrictMath.log(x0) * mult;
}
public double get(double x0, double x1, double x2) {
return StrictMath.log(x0) * mult;
}
public double get(double x0, double x1, double x2, double x3) {
return StrictMath.log(x0) * mult;
}
};
}
}
public abstract double get(double... x);
public double get() {
throw new IndexOutOfBoundsException("At least 1 argument required");
}
public abstract double get(double x0);
public abstract double get(double x0, double x1);
public abstract double get(double x0, double x1, double x2);
public abstract double get(double x0, double x1, double x2, double x3);
/**
* Returns a brief string description of this object.
*
* @return a brief string description of this object.
*/
public String toString() {
return "function f(x)=log(x) (base=" + this.base + ")";
}
}