Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmasson committed May 23, 2023
1 parent bb4deb9 commit c3d1af2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 42 deletions.
56 changes: 35 additions & 21 deletions build/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -3452,43 +3452,57 @@ function ln( x ) {

}

if ( isArbitrary(x) ) {
function arbitraryLn( x) {

var arb4 = 4n*arb1, arb10 = 10n*arb1;

// implicily testing for complex, positive real falls through

if ( x < 0n ) return { re: ln( -x ), im: onePi };
if ( x === arb1 ) return 0n;
if ( x.re === arb1 && x.im === 0n ) return { re: 0n, im: 0n };

if ( x === 0n || x.re === 0n && x.im === 0n )
throw Error( 'Arbitrary natural logarithm singularity' );

if ( x.re === 0n )
if ( x.im > 0n ) return { re: ln(x.im), im: halfPi };
else return { re: ln(-x.im), im: -halfPi };

// convergence near unit circle problematic
// scale argument radially and subtract scaling
// any number will work, convergence faster further out

if ( abs(x) < arb10 ) return sub( ln(mul(arb10,x)), ln10 );
if ( abs(x) < arb10 ) return sub( arbitraryLn(mul(arb10,x)), ln10 );

x = div( arb1, x );

var t2 = arbitraryTheta2(x);
var t3 = arbitraryTheta3(x);

var result = div( onePi, mul( arb4, arbitraryAGM( mul(t2,t2), mul(t3,t3) ) ) );
return div( onePi, mul( arb4, arbitraryAGM( mul(t2,t2), mul(t3,t3) ) ) );

}

if ( isArbitrary(x) ) {

if ( isComplex(x) ) {

if ( x.re === arb1 && x.im === 0n ) return { re: 0n, im: 0n };

if ( x.re === 0n && x.im === 0n )
throw Error( 'Arbitrary natural logarithm singularity' );

// directly set phase on complex axis
if ( x.re === 0n )
if ( x.im > 0n ) return { re: arbitraryLn(x.im), im: halfPi };
else return { re: arbitraryLn(-x.im), im: -halfPi };

var result = arbitraryLn(x);

// adjust imaginary part on left half-plane
if ( x.re < 0n ) {
if ( result.im > 0n ) result.im -= onePi;
else result.im += onePi;
}

return result;

// adjust imaginary part on left half-plane
if ( x.re < 0n ) {
if ( result.im > 0n ) result.im -= onePi;
else result.im += onePi;
}

return result;
if ( x === arb1 ) return 0n;
if ( x === 0n ) throw Error( 'Arbitrary natural logarithm singularity' );

if ( x < 0n ) return { re: arbitraryLn( -x ), im: onePi };

return arbitraryLn(x);

}

Expand Down
56 changes: 35 additions & 21 deletions src/functions/logarithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,43 +155,57 @@ function ln( x ) {

}

if ( isArbitrary(x) ) {
function arbitraryLn( x) {

var arb4 = 4n*arb1, arb10 = 10n*arb1;

// implicily testing for complex, positive real falls through

if ( x < 0n ) return { re: ln( -x ), im: onePi };
if ( x === arb1 ) return 0n;
if ( x.re === arb1 && x.im === 0n ) return { re: 0n, im: 0n };

if ( x === 0n || x.re === 0n && x.im === 0n )
throw Error( 'Arbitrary natural logarithm singularity' );

if ( x.re === 0n )
if ( x.im > 0n ) return { re: ln(x.im), im: halfPi };
else return { re: ln(-x.im), im: -halfPi };

// convergence near unit circle problematic
// scale argument radially and subtract scaling
// any number will work, convergence faster further out

if ( abs(x) < arb10 ) return sub( ln(mul(arb10,x)), ln10 );
if ( abs(x) < arb10 ) return sub( arbitraryLn(mul(arb10,x)), ln10 );

x = div( arb1, x );

var t2 = arbitraryTheta2(x);
var t3 = arbitraryTheta3(x);

var result = div( onePi, mul( arb4, arbitraryAGM( mul(t2,t2), mul(t3,t3) ) ) );
return div( onePi, mul( arb4, arbitraryAGM( mul(t2,t2), mul(t3,t3) ) ) );

}

if ( isArbitrary(x) ) {

if ( isComplex(x) ) {

if ( x.re === arb1 && x.im === 0n ) return { re: 0n, im: 0n };

if ( x.re === 0n && x.im === 0n )
throw Error( 'Arbitrary natural logarithm singularity' );

// directly set phase on complex axis
if ( x.re === 0n )
if ( x.im > 0n ) return { re: arbitraryLn(x.im), im: halfPi };
else return { re: arbitraryLn(-x.im), im: -halfPi };

var result = arbitraryLn(x);

// adjust imaginary part on left half-plane
if ( x.re < 0n ) {
if ( result.im > 0n ) result.im -= onePi;
else result.im += onePi;
}

return result;

// adjust imaginary part on left half-plane
if ( x.re < 0n ) {
if ( result.im > 0n ) result.im -= onePi;
else result.im += onePi;
}

return result;
if ( x === arb1 ) return 0n;
if ( x === 0n ) throw Error( 'Arbitrary natural logarithm singularity' );

if ( x < 0n ) return { re: arbitraryLn( -x ), im: onePi };

return arbitraryLn(x);

}

Expand Down

0 comments on commit c3d1af2

Please sign in to comment.