55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 79 | import { IContext } from './expressions'; 80 | import generate from './generate'; 81 | 82 | export default function parse<T extends IComplex>(Complex: IComplexConstructor<T>, text: string, context?: IContext<T>): T { 83 | const variable = generate(Complex, text); 84 | 85 | return variable(context!); 86 | } 87 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | | import from from '../functions/from'; 83 | import neg from '../functions/neg'; 84 | import not from '../functions/not'; 85 | 86 | export type Unary = typeof unaryLookup; 87 | 88 | export const unaryLookup = { 89 | '+': from, 90 | '-': neg, 91 | '~': not 92 | }; 93 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 | 1x 73 | 74 | 75 | 1x 76 | 77 | 1x 78 | 4x 79 | 80 | 81 | 82 | 4x 83 | 84 | | import absImpl from '../internal/absImpl'; 85 | import { IComplex, IComplexConstructor } from '../internal/complex'; 86 | import mask from '../internal/mask'; 87 | import getAbs from '../methods/getAbs'; 88 | 89 | export default function abs<T extends IComplex>(Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 90 | const zAbs = typeof z === 'number' 91 | ? absImpl(z, i) 92 | : getAbs(z); 93 | 94 | return new Complex(zAbs, 0, zAbs, 0, mask.HAS_ALL); 95 | } 96 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | | import argImpl from '../internal/argImpl'; 97 | import { IComplex, IComplexConstructor } from '../internal/complex'; 98 | import mask from '../internal/mask'; 99 | import getArg from '../methods/getArg'; 100 | 101 | export default function arg<T extends IComplex>(Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 102 | const zArg = typeof z === 'number' 103 | ? argImpl(z, i) 104 | : getArg(z); 105 | 106 | return new Complex( 107 | zArg, 108 | 0, 109 | Math.abs(zArg), 110 | zArg < 0 ? Math.PI : 0, 111 | mask.HAS_ALL 112 | ); 113 | } 114 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 95 | import add from '../methods/add'; 96 | import from from './from'; 97 | import log from './log'; 98 | import sqrt from './sqrt'; 99 | import square from './square'; 100 | 101 | export default function asinh<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 102 | const ONE = from(Complex, 1); 103 | 104 | const square1 = square(Complex, z, i); 105 | const add1 = add(Complex, ONE, square1); 106 | const sqrt1 = sqrt(Complex, add1); 107 | const add2 = add(Complex, sqrt1, z, i); 108 | 109 | return log(Complex, add2); 110 | } 111 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 99 | import mask from '../internal/mask'; 100 | import getImag from '../methods/getImag'; 101 | import getReal from '../methods/getReal'; 102 | 103 | export default function ceil<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 104 | let zReal: number 105 | let zImag: number; 106 | 107 | if (typeof z === 'number') { 108 | zReal = z; 109 | zImag = i; 110 | } else { 111 | zReal = getReal(z); 112 | zImag = getImag(z); 113 | } 114 | 115 | return new Complex(Math.ceil(zReal), Math.ceil(zImag), NaN, NaN, mask.HAS_CARTESIAN); 116 | } 117 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 99 | import mask from '../internal/mask'; 100 | import getImag from '../methods/getImag'; 101 | import getReal from '../methods/getReal'; 102 | 103 | export default function floor<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 104 | let zReal: number; 105 | let zImag: number; 106 | 107 | if (typeof z === 'number') { 108 | zReal = z; 109 | zImag = i; 110 | } else { 111 | zReal = getReal(z); 112 | zImag = getImag(z); 113 | } 114 | 115 | return new Complex(Math.floor(zReal), Math.floor(zImag), NaN, NaN, mask.HAS_CARTESIAN); 116 | } 117 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 97 | import imagImpl from '../internal/imagImpl'; 98 | import mask from '../internal/mask'; 99 | import getImag from '../methods/getImag'; 100 | 101 | export default function imag<T extends IComplex>(Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 102 | const zImag = typeof z === 'number' 103 | ? imagImpl(z, i) 104 | : getImag(z); 105 | 106 | return new Complex( 107 | zImag, 108 | 0, 109 | Math.abs(zImag), 110 | zImag < 0 ? Math.PI : 0, 111 | mask.HAS_ALL 112 | ); 113 | } 114 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 99 | import mask from '../internal/mask'; 100 | import getImag from '../methods/getImag'; 101 | import getReal from '../methods/getReal'; 102 | 103 | export default function not<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 104 | let zReal: number; 105 | let zImag: number; 106 | 107 | if (typeof z === 'number') { 108 | zReal = z; 109 | zImag = i; 110 | } else { 111 | zReal = getReal(z); 112 | zImag = getImag(z); 113 | } 114 | 115 | return new Complex(~zReal, ~zImag, NaN, NaN, mask.HAS_CARTESIAN); 116 | } 117 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 95 | import mask from '../internal/mask'; 96 | 97 | export default function polar<T extends IComplex> (Complex: IComplexConstructor<T>, abs: number, arg = 0): T { 98 | let zAbs: number; 99 | let zArg: number; 100 | 101 | if (abs < 0) { 102 | zAbs = -abs; 103 | zArg = arg + Math.PI; 104 | } else { 105 | zAbs = abs; 106 | zArg = arg; 107 | } 108 | 109 | return new Complex(NaN, NaN, zAbs, zArg, mask.HAS_POLAR); 110 | } 111 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 | 67 | 68 | 69 | 70 | 71 | 72 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 73 | import mask from '../internal/mask'; 74 | 75 | export default function random<T extends IComplex> (Complex: IComplexConstructor<T>): T { 76 | return new Complex(Math.random(), Math.random(), NaN, NaN, mask.HAS_CARTESIAN); 77 | } 78 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 97 | import mask from '../internal/mask'; 98 | import realImpl from '../internal/realImpl'; 99 | import getReal from '../methods/getReal'; 100 | 101 | export default function real<T extends IComplex>(Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 102 | const zReal = typeof z === 'number' 103 | ? realImpl(z, i) 104 | : getReal(z); 105 | 106 | return new Complex( 107 | zReal, 108 | 0, 109 | Math.abs(zReal), 110 | zReal < 0 ? Math.PI : 0, 111 | mask.HAS_ALL 112 | ); 113 | } 114 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 99 | import mask from '../internal/mask'; 100 | import getImag from '../methods/getImag'; 101 | import getReal from '../methods/getReal'; 102 | 103 | export default function round<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 104 | let zReal: number; 105 | let zImag: number; 106 | 107 | if (typeof z === 'number') { 108 | zReal = z; 109 | zImag = i; 110 | } else { 111 | zReal = getReal(z); 112 | zImag = getImag(z); 113 | } 114 | 115 | return new Complex(Math.round(zReal), Math.round(zImag), NaN, NaN, mask.HAS_CARTESIAN); 116 | } 117 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | | import { IComplex, IComplexConstructor } from '../internal/complex'; 99 | import mask from '../internal/mask'; 100 | import getImag from '../methods/getImag'; 101 | import getReal from '../methods/getReal'; 102 | 103 | export default function trunc<T extends IComplex> (Complex: IComplexConstructor<T>, z: IComplex | number, i = 0): T { 104 | let zReal: number; 105 | let zImag: number; 106 | 107 | if (typeof z === 'number') { 108 | zReal = z; 109 | zImag = i; 110 | } else { 111 | zReal = getReal(z); 112 | zImag = getImag(z); 113 | } 114 | 115 | return new Complex(Math.trunc(zReal), Math.trunc(zImag), NaN, NaN, mask.HAS_CARTESIAN); 116 | } 117 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |File | 64 |65 | | Statements | 66 |67 | | Branches | 68 |69 | | Functions | 70 |71 | | Lines | 72 |73 | |
---|---|---|---|---|---|---|---|---|---|
complex.ts | 77 |
78 |
79 | |
80 | 0% | 81 |0/125 | 82 |0% | 83 |0/1 | 84 |0% | 85 |0/56 | 86 |0% | 87 |0/125 | 88 |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 | 1x 71 | 3x 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | | export default function absImpl (real: number, imag: number): number { 81 | return ( 82 | // if z is real, abs = |real| 83 | imag === 0 ? Math.abs(real) 84 | // if z is imag, abs = |imag| 85 | : real === 0 ? Math.abs(imag) 86 | // else abs = |z| 87 | : Math.hypot(real, imag) 88 | ); 89 | } 90 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | | export default function argImpl (real: number, imag: number): number { 81 | return ( 82 | // if z is real, if z is negative, arg = pi, else arg = 0 83 | imag === 0 ? (real < 0 ? Math.PI : 0) 84 | // if z is imag, arg = sign(imag) * pi / 2 85 | : real === 0 ? (imag < 0 ? -0.5 : 0.5) * Math.PI 86 | // else arg = atan(imag / real) 87 | : Math.atan2(imag, real) 88 | ); 89 | } 90 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | | export default function imagImpl (abs: number, arg: number): number { 77 | return ( 78 | // if z is real, imag = 0 79 | arg === 0 || arg === Math.PI ? 0 80 | // else imag = abs * sin(arg) 81 | : abs * Math.sin(arg) 82 | ); 83 | } 84 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | | export default function realImpl (abs: number, arg: number): number { 81 | return ( 82 | // if z is positive, real = abs 83 | arg === 0 ? abs 84 | // if z is negative, real = -abs 85 | : arg === Math.PI ? -abs 86 | // else real = abs * cos(arg) 87 | : abs * Math.cos(arg) 88 | ); 89 | } 90 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 | 1x 73 | 74 | 75 | 76 | 1x 77 | 2x 78 | 1x 79 | 1x 80 | 81 | 82 | 2x 83 | 84 | | import absImpl from '../internal/absImpl'; 85 | import { IComplex } from '../internal/complex'; 86 | import mask from '../internal/mask'; 87 | 88 | export default function getAbs (z: IComplex): number { 89 | if (!(z._mask & mask.HAS_ABS)) { 90 | z._abs = absImpl(z._real, z._imag); 91 | z._mask |= mask.HAS_ABS; 92 | } 93 | 94 | return z._abs; 95 | } 96 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | | import argImpl from '../internal/argImpl'; 85 | import { IComplex } from '../internal/complex'; 86 | import mask from '../internal/mask'; 87 | 88 | export default function getArg (z: IComplex): number { 89 | if (!(z._mask & mask.HAS_ARG)) { 90 | z._arg = argImpl(z._real, z._imag); 91 | z._mask |= mask.HAS_ARG; 92 | } 93 | 94 | return z._arg; 95 | } 96 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | | import { IComplex } from '../internal/complex'; 85 | import imagImpl from '../internal/imagImpl'; 86 | import mask from '../internal/mask'; 87 | 88 | export default function getImag (z: IComplex): number { 89 | if (!(z._mask & mask.HAS_IMAG)) { 90 | z._imag = imagImpl(z._abs, z._arg); 91 | z._mask |= mask.HAS_IMAG; 92 | } 93 | 94 | return z._imag; 95 | } 96 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | | import { IComplex } from '../internal/complex'; 85 | import mask from '../internal/mask'; 86 | import realImpl from '../internal/realImpl'; 87 | 88 | export default function getReal (z: IComplex): number { 89 | if (!(z._mask & mask.HAS_REAL)) { 90 | z._real = realImpl(z._abs, z._arg); 91 | z._mask |= mask.HAS_REAL; 92 | } 93 | 94 | return z._real; 95 | } 96 | |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 |1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | | import trunc from '../functions/trunc'; 87 | import { IComplex, IComplexConstructor } from '../internal/complex'; 88 | import div from './div'; 89 | import mul from './mul'; 90 | import sub from './sub'; 91 | 92 | export default function mod<T extends IComplex> (Complex: IComplexConstructor<T>, lhs: IComplex, r: IComplex | number, i = 0): T { 93 | // lhs % rhs = lhs - (trunc(lhs / rhs) * rhs) 94 | const q = div(Complex, lhs, r, i); 95 | const p = mul(Complex, trunc(Complex, q), r, i); 96 | 97 | return sub(Complex, lhs, p); 98 | } 99 | |