本文整理汇总了Java中org.apache.commons.math3.exception.util.LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N属性的典型用法代码示例。如果您正苦于以下问题:Java LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N属性的具体用法?Java LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N怎么用?Java LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.commons.math3.exception.util.LocalizedFormats
的用法示例。
在下文中一共展示了LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nthRoot
/**
* Computes the n-th roots of this complex number.
* The nth roots are defined by the formula:
* <pre>
* <code>
* z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))
* </code>
* </pre>
* for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
* are respectively the {@link #abs() modulus} and
* {@link #getArgument() argument} of this complex number.
* <p>
* If one or both parts of this complex number is NaN, a list with just
* one element, {@link #NaN} is returned.
* if neither part is NaN, but at least one part is infinite, the result
* is a one-element list containing {@link #INF}.
*
* @param n Degree of root.
* @return a List of all {@code n}-th roots of {@code this}.
* @throws NotPositiveException if {@code n <= 0}.
* @since 2.0
*/
public List<Complex> nthRoot(int n) throws NotPositiveException {
if (n <= 0) {
throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
n);
}
final List<Complex> result = new ArrayList<Complex>();
if (isNaN) {
result.add(NaN);
return result;
}
if (isInfinite()) {
result.add(INF);
return result;
}
// nth root of abs -- faster / more accurate to use a solver here?
final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);
// Compute nth roots of complex number with k = 0, 1, ... n-1
final double nthPhi = getArgument() / n;
final double slice = 2 * FastMath.PI / n;
double innerPart = nthPhi;
for (int k = 0; k < n ; k++) {
// inner part
final double realPart = nthRootOfAbs * FastMath.cos(innerPart);
final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart);
result.add(createComplex(realPart, imaginaryPart));
innerPart += slice;
}
return result;
}