/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import java.io.Serializable; /** * A helper class for the computation and caching of the {@code n}-th roots of unity. * * @since 3.0 */ public class RootsOfUnity implements Serializable { /** Serializable version id. */ private static final long serialVersionUID = 20120201L; /** Number of roots of unity. */ private int omegaCount; /** Real part of the roots. */ private double[] omegaReal; /** * Imaginary part of the {@code n}-th roots of unity, for positive values of {@code n}. In this * array, the roots are stored in counter-clockwise order. */ private double[] omegaImaginaryCounterClockwise; /** * Imaginary part of the {@code n}-th roots of unity, for negative values of {@code n}. In this * array, the roots are stored in clockwise order. */ private double[] omegaImaginaryClockwise; /** * {@code true} if {@link #computeRoots(int)} was called with a positive value of its argument * {@code n}. In this case, counter-clockwise ordering of the roots of unity should be used. */ private boolean isCounterClockWise; /** Build an engine for computing the {@code n}-th roots of unity. */ public RootsOfUnity() { omegaCount = 0; omegaReal = null; omegaImaginaryCounterClockwise = null; omegaImaginaryClockwise = null; isCounterClockWise = true; } /** * Returns {@code true} if {@link #computeRoots(int)} was called with a positive value of its * argument {@code n}. If {@code true}, then counter-clockwise ordering of the roots of unity * should be used. * * @return {@code true} if the roots of unity are stored in counter-clockwise order * @throws MathIllegalStateException if no roots of unity have been computed yet */ public synchronized boolean isCounterClockWise() throws MathIllegalStateException { if (omegaCount == 0) { throw new MathIllegalStateException(LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); } return isCounterClockWise; } /** * Computes the {@code n}-th roots of unity. The roots are stored in {@code omega[]}, such that * {@code omega[k] = w ^ k}, where {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} * and {@code i = sqrt(-1)}. * *
Note that {@code n} can be positive of negative * *