Skip to content

Commit

Permalink
refactor: 🔥 move derivative function to ootk
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 17, 2024
1 parent 21c2b7b commit bd39ed4
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 27 deletions.
22 changes: 1 addition & 21 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable require-jsdoc */
import { AngularDiameterMethod } from '../enums/AngularDiameterMethod';
import { AngularDistanceMethod } from '../enums/AngularDistanceMethod';
import { DifferentiableFunction, EcfVec3, Kilometers, Radians, SpaceObjectType } from '../types/types';
import { EcfVec3, Kilometers, Radians, SpaceObjectType } from '../types/types';
import { angularVelocityOfEarth, cKmPerSec } from './constants';

/**
Expand Down Expand Up @@ -299,26 +299,6 @@ export function covariance(a: number[], b: number[], isSample = false): number {
return result / (n - m);
}

/**
* Calculates the derivative of a differentiable function.
* @param f The differentiable function.
* @param h The step size for numerical differentiation. Default value is 1e-3.
* @returns The derivative function.
*/
export function derivative(f: DifferentiableFunction, h = 1e-3): DifferentiableFunction {
/**
* @param x The value at which to calculate the derivative.
* @returns The derivative of the function at the given value.
*/
function df(x: number): number {
const hh = h * 0.5;

return (f(x + hh) - f(x - hh)) / h;
}

return df;
}

/**
* Calculates the gamma function of a number.
* @param n - The input number.
Expand Down
6 changes: 0 additions & 6 deletions test/utils/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
createVec,
csch,
deg2rad,
derivative,
gamma,
linearInterpolate,
log10,
Expand Down Expand Up @@ -103,11 +102,6 @@ describe('functions', () => {
expect(gamma(1)).toMatchSnapshot();
});

// derivative
it('should be calculate derivative', () => {
expect(derivative((x: number) => x * x, 1)).toMatchSnapshot();
});

// covariance
it('should be calculate covariance', () => {
expect(covariance([1, 2, 3], [1, 2, 3])).toMatchSnapshot();
Expand Down

0 comments on commit bd39ed4

Please sign in to comment.