org.ddogleg.complex.ComplexMath64F Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ddogleg Show documentation
Show all versions of ddogleg Show documentation
DDogleg Numerics is a high performance Java library for non-linear optimization, robust model fitting, polynomial root finding, sorting, and more.
/*
* Copyright (c) 2012-2013, Peter Abeles. All Rights Reserved.
*
* This file is part of DDogleg (http://ddogleg.org).
*
* Licensed 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.ddogleg.complex;
import org.ejml.data.Complex64F;
/**
* Basic math operations on complex numbers.
*
* @author Peter Abeles
*/
public class ComplexMath64F
{
/**
* Complex conjugate
* @param input Input complex number
* @param conj Complex conjugate of the input number
*/
public static void conj( Complex64F input , Complex64F conj ) {
conj.real = input.real;
conj.imaginary = -input.imaginary;
}
/**
*
* Addition: result = a + b
*
*
* @param a Complex number. Not modified.
* @param b Complex number. Not modified.
* @param result Storage for output
*/
public static void plus( Complex64F a , Complex64F b , Complex64F result ) {
result.real = a.real + b.real;
result.imaginary = a.imaginary + b.imaginary;
}
/**
*
* Subtraction: result = a - b
*
*
* @param a Complex number. Not modified.
* @param b Complex number. Not modified.
* @param result Storage for output
*/
public static void minus( Complex64F a , Complex64F b , Complex64F result ) {
result.real = a.real - b.real;
result.imaginary = a.imaginary - b.imaginary;
}
/**
*
* Multiplication: result = a * b
*
*
* @param a Complex number. Not modified.
* @param b Complex number. Not modified.
* @param result Storage for output
*/
public static void mult( Complex64F a , Complex64F b , Complex64F result ) {
result.real = a.real * b.real - a.imaginary*b.imaginary;
result.imaginary = a.real*b.imaginary + a.imaginary*b.real;
}
/**
*
* Division: result = a / b
*
*
* @param a Complex number. Not modified.
* @param b Complex number. Not modified.
* @param result Storage for output
*/
public static void div( Complex64F a , Complex64F b , Complex64F result ) {
double norm = b.getMagnitude2();
result.real = (a.real * b.real + a.imaginary*b.imaginary)/norm;
result.imaginary = (a.imaginary*b.real - a.real*b.imaginary)/norm;
}
/**
*
* Converts a complex number into polar notation.
*
*
* @param input Standard notation
* @param output Polar notation
*/
public static void convert( Complex64F input , ComplexPolar64F output ) {
output.r = input.getMagnitude();
output.theta = Math.atan2(input.imaginary, input.real);
}
/**
*
* Converts a complex number in polar notation into standard notation.
*
*
* @param input Standard notation
* @param output Polar notation
*/
public static void convert( ComplexPolar64F input , Complex64F output ) {
output.real = input.r*Math.cos(input.theta);
output.imaginary = input.r*Math.sin(input.theta);
}
/**
* Division in polar notation.
*
* @param a Complex number in polar notation. Not modified.
* @param b Complex number in polar notation. Not modified.
* @param result Storage for output.
*/
public static void mult( ComplexPolar64F a , ComplexPolar64F b , ComplexPolar64F result )
{
result.r = a.r*b.r;
result.theta = a.theta + b.theta;
}
/**
* Division in polar notation.
*
* @param a Complex number in polar notation. Not modified.
* @param b Complex number in polar notation. Not modified.
* @param result Storage for output.
*/
public static void div( ComplexPolar64F a , ComplexPolar64F b , ComplexPolar64F result )
{
result.r = a.r/b.r;
result.theta = a.theta - b.theta;
}
/**
* Computes the power of a complex number in polar notation
*
* @param a Complex number
* @param N Power it is to be multiplied by
* @param result Result
*/
public static void pow( ComplexPolar64F a , int N , ComplexPolar64F result )
{
result.r = Math.pow(a.r,N);
result.theta = N*a.theta;
}
/**
* Computes the Nth root of a complex number in polar notation. There are
* N distinct Nth roots.
*
* @param a Complex number
* @param N The root's magnitude
* @param k Specifies which root. 0 ≤ k < N
* @param result Computed root
*/
public static void root( ComplexPolar64F a , int N , int k , ComplexPolar64F result )
{
result.r = Math.pow(a.r,1.0/N);
result.theta = (a.theta + 2.0*k*Math.PI)/N;
}
/**
* Computes the Nth root of a complex number. There are
* N distinct Nth roots.
*
* @param a Complex number
* @param N The root's magnitude
* @param k Specifies which root. 0 ≤ k < N
* @param result Computed root
*/
public static void root( Complex64F a , int N , int k , Complex64F result )
{
double r = a.getMagnitude();
double theta = Math.atan2(a.imaginary,a.real);
r = Math.pow(r,1.0/N);
theta = (theta + 2.0*k*Math.PI)/N;
result.real = r*Math.cos(theta);
result.imaginary = r*Math.sin(theta);
}
}