Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formula): supplement IMCOTH/IMLOG/IMTANH formula #3909

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions packages/engine-formula/src/basics/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,25 @@ export class Complex {
}
}

Coth(): number | string {
if (!Number.isFinite(Math.sinh(this._realNum)) || !Number.isFinite(Math.cosh(this._realNum))) {
this._isError = true;
return '';
}

if (this._iNum) {
const den = Decimal.cosh(this._realNum * 2).sub(Decimal.cos(this._iNum * 2));
const realNum = Decimal.sinh(this._realNum * 2).div(den).toNumber();
const iNum = Decimal.sin(this._iNum * 2).div(den).negated().toNumber();

return Complex.getComplex(realNum, iNum, this._suffix);
} else {
const realNum = new Decimal(1).div(Decimal.tanh(this._realNum)).toNumber();

return Complex.getComplex(realNum, this._iNum, this._suffix);
}
}

Csc(): number | string {
if (this._iNum) {
const den = Decimal.cosh(this._iNum * 2).sub(Decimal.cos(this._realNum * 2));
Expand Down Expand Up @@ -377,7 +396,7 @@ export class Complex {
return Complex.getComplex(realNum, iNum, this._suffix);
}

Log10(): number | string {
Log(base: number): number | string {
const abs = Decimal.sqrt(Decimal.pow(this._realNum, 2).add(Decimal.pow(this._iNum, 2)));
const Decimal_realNum1 = Decimal.ln(abs);
let Decimal_iNum1 = Decimal.acos(new Decimal(this._realNum).div(abs));
Expand All @@ -386,29 +405,16 @@ export class Complex {
Decimal_iNum1 = Decimal_iNum1.negated();
}

const Decimal_realNum2 = Decimal.ln(10);
const Decimal_realNum2 = Decimal.ln(base);
const Decimal_iNum2 = new Decimal(0);

const den = Decimal_realNum2.mul(Decimal_realNum2).add(Decimal_iNum2.mul(Decimal_iNum2));
const realNum = Decimal_realNum1.mul(Decimal_realNum2).add(Decimal_iNum1.mul(Decimal_iNum2)).div(den).toNumber();
const iNum = Decimal_iNum1.mul(Decimal_realNum2).sub(Decimal_realNum1.mul(Decimal_iNum2)).div(den).toNumber();

return Complex.getComplex(realNum, iNum, this._suffix);
}

Log2(): number | string {
const abs = Decimal.sqrt(Decimal.pow(this._realNum, 2).add(Decimal.pow(this._iNum, 2)));
const Decimal_realNum1 = Decimal.ln(abs);
let Decimal_iNum1 = Decimal.acos(new Decimal(this._realNum).div(abs));

if (this._iNum < 0) {
Decimal_iNum1 = Decimal_iNum1.negated();
if (den.eq(0)) {
this._isError = true;
return '';
}

const Decimal_realNum2 = Decimal.ln(2);
const Decimal_iNum2 = new Decimal(0);

const den = Decimal_realNum2.mul(Decimal_realNum2).add(Decimal_iNum2.mul(Decimal_iNum2));
const realNum = Decimal_realNum1.mul(Decimal_realNum2).add(Decimal_iNum1.mul(Decimal_iNum2)).div(den).toNumber();
const iNum = Decimal_iNum1.mul(Decimal_realNum2).sub(Decimal_realNum1.mul(Decimal_iNum2)).div(den).toNumber();

Expand Down Expand Up @@ -580,4 +586,23 @@ export class Complex {
return Complex.getComplex(realNum, this._iNum, this._suffix);
}
}

Tanh(): number | string {
if (!Number.isFinite(Math.sinh(this._realNum)) || !Number.isFinite(Math.cosh(this._realNum))) {
this._isError = true;
return '';
}

if (this._iNum) {
const den = Decimal.cosh(this._realNum * 2).add(Decimal.cos(this._iNum * 2));
const realNum = Decimal.sinh(this._realNum * 2).div(den).toNumber();
const iNum = Decimal.sin(this._iNum * 2).div(den).toNumber();

return Complex.getComplex(realNum, iNum, this._suffix);
} else {
const realNum = Decimal.tanh(this._realNum).toNumber();

return Complex.getComplex(realNum, this._iNum, this._suffix);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { FUNCTION_NAMES_ENGINEERING } from './function-names';
import { Besseli } from './besseli';
import { Besselj } from './besselj';
import { Besselk } from './besselk';
Expand All @@ -37,6 +36,7 @@ import { Erf } from './erf';
import { ErfPrecise } from './erf-precise';
import { Erfc } from './erfc';
import { ErfcPrecise } from './erfc-precise';
import { FUNCTION_NAMES_ENGINEERING } from './function-names';
import { Gestep } from './gestep';
import { Hex2bin } from './hex2bin';
import { Hex2dec } from './hex2dec';
Expand All @@ -48,13 +48,15 @@ import { Imconjugate } from './imconjugate';
import { Imcos } from './imcos';
import { Imcosh } from './imcosh';
import { Imcot } from './imcot';
import { Imcoth } from './imcoth';
import { Imcsc } from './imcsc';
import { Imcsch } from './imcsch';
import { Imdiv } from './imdiv';
import { Imexp } from './imexp';
import { Imln } from './imln';
import { Imlog10 } from './imlog10';
import { Imlog } from './imlog';
import { Imlog2 } from './imlog2';
import { Imlog10 } from './imlog10';
import { Impower } from './impower';
import { Improduct } from './improduct';
import { Imreal } from './imreal';
Expand All @@ -66,6 +68,7 @@ import { Imsqrt } from './imsqrt';
import { Imsub } from './imsub';
import { Imsum } from './imsum';
import { Imtan } from './imtan';
import { Imtanh } from './imtanh';
import { Oct2bin } from './oct2bin';
import { Oct2dec } from './oct2dec';
import { Oct2hex } from './oct2hex';
Expand Down Expand Up @@ -104,11 +107,13 @@ export const functionEngineering = [
[Imcos, FUNCTION_NAMES_ENGINEERING.IMCOS],
[Imcosh, FUNCTION_NAMES_ENGINEERING.IMCOSH],
[Imcot, FUNCTION_NAMES_ENGINEERING.IMCOT],
[Imcoth, FUNCTION_NAMES_ENGINEERING.IMCOTH],
[Imcsc, FUNCTION_NAMES_ENGINEERING.IMCSC],
[Imcsch, FUNCTION_NAMES_ENGINEERING.IMCSCH],
[Imdiv, FUNCTION_NAMES_ENGINEERING.IMDIV],
[Imexp, FUNCTION_NAMES_ENGINEERING.IMEXP],
[Imln, FUNCTION_NAMES_ENGINEERING.IMLN],
[Imlog, FUNCTION_NAMES_ENGINEERING.IMLOG],
[Imlog10, FUNCTION_NAMES_ENGINEERING.IMLOG10],
[Imlog2, FUNCTION_NAMES_ENGINEERING.IMLOG2],
[Impower, FUNCTION_NAMES_ENGINEERING.IMPOWER],
Expand All @@ -122,6 +127,7 @@ export const functionEngineering = [
[Imsub, FUNCTION_NAMES_ENGINEERING.IMSUB],
[Imsum, FUNCTION_NAMES_ENGINEERING.IMSUM],
[Imtan, FUNCTION_NAMES_ENGINEERING.IMTAN],
[Imtanh, FUNCTION_NAMES_ENGINEERING.IMTANH],
[Oct2bin, FUNCTION_NAMES_ENGINEERING.OCT2BIN],
[Oct2dec, FUNCTION_NAMES_ENGINEERING.OCT2DEC],
[Oct2hex, FUNCTION_NAMES_ENGINEERING.OCT2HEX],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ export enum FUNCTION_NAMES_ENGINEERING {
IMCOS = 'IMCOS',
IMCOSH = 'IMCOSH',
IMCOT = 'IMCOT',
IMCOTH = 'IMCOTH',
IMCSC = 'IMCSC',
IMCSCH = 'IMCSCH',
IMDIV = 'IMDIV',
IMEXP = 'IMEXP',
IMLN = 'IMLN',
IMLOG = 'IMLOG',
IMLOG10 = 'IMLOG10',
IMLOG2 = 'IMLOG2',
IMPOWER = 'IMPOWER',
Expand All @@ -66,6 +68,7 @@ export enum FUNCTION_NAMES_ENGINEERING {
IMSUB = 'IMSUB',
IMSUM = 'IMSUM',
IMTAN = 'IMTAN',
IMTANH = 'IMTANH',
OCT2BIN = 'OCT2BIN',
OCT2DEC = 'OCT2DEC',
OCT2HEX = 'OCT2HEX',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* 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.
*/

import { describe, expect, it } from 'vitest';

import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { FUNCTION_NAMES_ENGINEERING } from '../../function-names';
import { Imcoth } from '../index';

describe('Test imcoth function', () => {
const testFunction = new Imcoth(FUNCTION_NAMES_ENGINEERING.IMCOTH);

describe('Imcoth', () => {
it('Value is normal number', () => {
const inumber = StringValueObject.create('5+12i');
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe('1.00003851275523+0.0000822295549932979i');
});

it('Value is large numbers', () => {
const inumber = NumberValueObject.create(25698432);
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(ErrorType.NUM);
});

it('Value is number string', () => {
const inumber = StringValueObject.create('1.5');
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(1.10479139298251);
});

it('Value is normal string', () => {
const inumber = StringValueObject.create('test');
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(ErrorType.NUM);
});

it('Value is boolean', () => {
const inumber = BooleanValueObject.create(true);
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(ErrorType.VALUE);
});

it('Value is blank cell', () => {
const inumber = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null],
]),
rowCount: 1,
columnCount: 1,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(ErrorType.NUM);
});

it('Value is error', () => {
const inumber = ErrorValueObject.create(ErrorType.NAME);
const result = testFunction.calculate(inumber);
expect(result.getValue()).toBe(ErrorType.NAME);
});

it('Value is array', () => {
const inumber = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[1, ' ', 1.23, true, false, null],
[0, '100', '2.34', 'test', -3, ErrorType.NAME],
]),
rowCount: 2,
columnCount: 6,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = testFunction.calculate(inumber);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* 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.
*/

import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';

export class Imcoth extends BaseFunction {
override minParams = 1;

override maxParams = 1;

override calculate(inumber: BaseValueObject): BaseValueObject {
const { isError, errorObject, variants } = checkVariantsErrorIsArrayOrBoolean(inumber);

if (isError) {
return errorObject as ErrorValueObject;
}

const [inumberObject] = variants as BaseValueObject[];

const inumberValue = `${inumberObject.getValue()}`;

const complex = new Complex(inumberValue);

if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
}

if (complex.getRealNum() === 0 && complex.getINum() === 0) {
return ErrorValueObject.create(ErrorType.NUM);
}

const result = complex.Coth();

if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
}

if (typeof result === 'number' || isRealNum(result)) {
return NumberValueObject.create(+result);
}

return StringValueObject.create(result);
}
}
Loading
Loading