mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Implemented methods asin, acos, atan
This commit is contained in:
parent
f798687804
commit
27b96f31e9
199
lib/math.js
199
lib/math.js
@ -929,7 +929,7 @@ function sin(x) {
|
||||
return Math.sin(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return new Complex(
|
||||
0.5 * Math.sin(x.re) * (Math.exp(-x.im) + Math.exp( x.im)),
|
||||
0.5 * Math.cos(x.re) * (Math.exp( x.im) - Math.exp(-x.im))
|
||||
@ -975,6 +975,127 @@ sin.doc = {
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the inverse sine of a value, asin(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function asin(x) {
|
||||
if (isNumber(x)) {
|
||||
if (x >= -1 && x <= 1) {
|
||||
return Math.asin(x);
|
||||
}
|
||||
else {
|
||||
return asin(new Complex(x, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// asin(z) = -i*log(iz + sqrt(1-z^2))
|
||||
var re = x.re;
|
||||
var im = x.im;
|
||||
var temp1 = new Complex(
|
||||
im * im - re * re + 1.0,
|
||||
-2.0 * re * im
|
||||
);
|
||||
|
||||
var temp2 = sqrt(temp1);
|
||||
var temp3 = new Complex(
|
||||
temp2.re - im,
|
||||
temp2.im + re
|
||||
);
|
||||
|
||||
var temp4 = log(temp3);
|
||||
|
||||
return new Complex(temp4.im, -temp4.re);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('asin', x);
|
||||
}
|
||||
|
||||
math.asin = asin;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
asin.doc = {
|
||||
'name': 'asin',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'asin(x)'
|
||||
],
|
||||
'description': 'Compute the inverse sine of a value in radians.',
|
||||
'examples': [
|
||||
'asin(0.5)',
|
||||
'asin(sin(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'sin',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the inverse tangent of a value, atan(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function atan(x) {
|
||||
if (isNumber(x)) {
|
||||
return Math.atan(x);
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// atan(z) = 1/2 * i * (ln(1-iz) - ln(1+iz))
|
||||
var re = x.re;
|
||||
var im = x.im;
|
||||
var den = re * re + (1.0 - im) * (1.0 - im);
|
||||
|
||||
var temp1 = new Complex(
|
||||
(1.0 - im * im - re * re) / den,
|
||||
(-2.0 * re) / den
|
||||
);
|
||||
var temp2 = log(temp1);
|
||||
|
||||
return new Complex(
|
||||
-0.5 * temp2.im,
|
||||
0.5 * temp2.re
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('atan', x);
|
||||
}
|
||||
|
||||
math.atan = atan;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
atan.doc = {
|
||||
'name': 'atan',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'atan(x)'
|
||||
],
|
||||
'description': 'Compute the inverse tangent of a value in radians.',
|
||||
'examples': [
|
||||
'atan(0.5)',
|
||||
'atan(tan(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'tan',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the cosine of a value, cos(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
@ -985,7 +1106,7 @@ function cos(x) {
|
||||
return Math.cos(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
// cos(z) = (exp(iz) + exp(-iz)) / 2
|
||||
return new Complex(
|
||||
0.5 * Math.cos(x.re) * (Math.exp(-x.im) + Math.exp(x.im)),
|
||||
@ -1042,7 +1163,7 @@ function tan(x) {
|
||||
return Math.tan(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var den = Math.exp(-4.0 * x.im) +
|
||||
2.0 * Math.exp(-2.0 * x.im) * Math.cos(2.0 * x.re) +
|
||||
1.0;
|
||||
@ -1091,6 +1212,70 @@ tan.doc = {
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the inverse cosine of a value, acos(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function acos(x) {
|
||||
if (isNumber(x)) {
|
||||
if (x >= -1 && x <= 1) {
|
||||
return Math.acos(x);
|
||||
}
|
||||
else {
|
||||
return acos(new Complex(x, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// acos(z) = 0.5*pi + i*log(iz + sqrt(1-z^2))
|
||||
var temp1 = new Complex(
|
||||
x.im * x.im - x.re * x.re + 1.0,
|
||||
-2.0 * x.re * x.im
|
||||
);
|
||||
var temp2 = sqrt(temp1);
|
||||
var temp3 = new Complex(
|
||||
temp2.re - x.im,
|
||||
temp2.im + x.re
|
||||
);
|
||||
var temp4 = log(temp3);
|
||||
|
||||
// 0.5*pi = 1.5707963267948966192313216916398
|
||||
return new Complex(
|
||||
1.57079632679489661923 - temp4.im,
|
||||
temp4.re
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('acos', x);
|
||||
}
|
||||
|
||||
math.acos = acos;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
acos.doc = {
|
||||
'name': 'acos',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'acos(x)'
|
||||
],
|
||||
'description': 'Compute the inverse cosine of a value in radians.',
|
||||
'examples': [
|
||||
'acos(0.5)',
|
||||
'acos(cos(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'cos',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the exponent of a value, exp(x)
|
||||
* @param {Number | Complex} x
|
||||
@ -1100,7 +1285,7 @@ function exp (x) {
|
||||
if (isNumber(x)) {
|
||||
return Math.exp(x);
|
||||
}
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var r = Math.exp(x.re);
|
||||
return new Complex(
|
||||
r * Math.cos(x.im),
|
||||
@ -1154,7 +1339,7 @@ function sqrt (x) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var r = Math.sqrt(x.re * x.re + x.im * x.im);
|
||||
if (x.im >= 0.0) {
|
||||
return new Complex(
|
||||
@ -1211,7 +1396,7 @@ function abs(x) {
|
||||
return Math.abs(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return Math.sqrt(x.re * x.re + x.im * x.im);
|
||||
}
|
||||
|
||||
@ -1256,7 +1441,7 @@ function log(x) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return new Complex (
|
||||
Math.log(Math.sqrt(x.re * x.re + x.im * x.im)),
|
||||
Math.atan2(x.im, x.re)
|
||||
|
||||
2
lib/math.min.js
vendored
2
lib/math.min.js
vendored
File diff suppressed because one or more lines are too long
@ -8,7 +8,7 @@ function abs(x) {
|
||||
return Math.abs(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return Math.sqrt(x.re * x.re + x.im * x.im);
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ function exp (x) {
|
||||
if (isNumber(x)) {
|
||||
return Math.exp(x);
|
||||
}
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var r = Math.exp(x.re);
|
||||
return new Complex(
|
||||
r * Math.cos(x.im),
|
||||
|
||||
@ -14,7 +14,7 @@ function log(x) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return new Complex (
|
||||
Math.log(Math.sqrt(x.re * x.re + x.im * x.im)),
|
||||
Math.atan2(x.im, x.re)
|
||||
|
||||
@ -13,7 +13,7 @@ function sqrt (x) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var r = Math.sqrt(x.re * x.re + x.im * x.im);
|
||||
if (x.im >= 0.0) {
|
||||
return new Complex(
|
||||
|
||||
63
src/function/trigonometry/acos.js
Normal file
63
src/function/trigonometry/acos.js
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Calculate the inverse cosine of a value, acos(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function acos(x) {
|
||||
if (isNumber(x)) {
|
||||
if (x >= -1 && x <= 1) {
|
||||
return Math.acos(x);
|
||||
}
|
||||
else {
|
||||
return acos(new Complex(x, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// acos(z) = 0.5*pi + i*log(iz + sqrt(1-z^2))
|
||||
var temp1 = new Complex(
|
||||
x.im * x.im - x.re * x.re + 1.0,
|
||||
-2.0 * x.re * x.im
|
||||
);
|
||||
var temp2 = sqrt(temp1);
|
||||
var temp3 = new Complex(
|
||||
temp2.re - x.im,
|
||||
temp2.im + x.re
|
||||
);
|
||||
var temp4 = log(temp3);
|
||||
|
||||
// 0.5*pi = 1.5707963267948966192313216916398
|
||||
return new Complex(
|
||||
1.57079632679489661923 - temp4.im,
|
||||
temp4.re
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('acos', x);
|
||||
}
|
||||
|
||||
math.acos = acos;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
acos.doc = {
|
||||
'name': 'acos',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'acos(x)'
|
||||
],
|
||||
'description': 'Compute the inverse cosine of a value in radians.',
|
||||
'examples': [
|
||||
'acos(0.5)',
|
||||
'acos(cos(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'cos',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
63
src/function/trigonometry/asin.js
Normal file
63
src/function/trigonometry/asin.js
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Calculate the inverse sine of a value, asin(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function asin(x) {
|
||||
if (isNumber(x)) {
|
||||
if (x >= -1 && x <= 1) {
|
||||
return Math.asin(x);
|
||||
}
|
||||
else {
|
||||
return asin(new Complex(x, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// asin(z) = -i*log(iz + sqrt(1-z^2))
|
||||
var re = x.re;
|
||||
var im = x.im;
|
||||
var temp1 = new Complex(
|
||||
im * im - re * re + 1.0,
|
||||
-2.0 * re * im
|
||||
);
|
||||
|
||||
var temp2 = sqrt(temp1);
|
||||
var temp3 = new Complex(
|
||||
temp2.re - im,
|
||||
temp2.im + re
|
||||
);
|
||||
|
||||
var temp4 = log(temp3);
|
||||
|
||||
return new Complex(temp4.im, -temp4.re);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('asin', x);
|
||||
}
|
||||
|
||||
math.asin = asin;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
asin.doc = {
|
||||
'name': 'asin',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'asin(x)'
|
||||
],
|
||||
'description': 'Compute the inverse sine of a value in radians.',
|
||||
'examples': [
|
||||
'asin(0.5)',
|
||||
'asin(sin(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'sin',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
56
src/function/trigonometry/atan.js
Normal file
56
src/function/trigonometry/atan.js
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Calculate the inverse tangent of a value, atan(x)
|
||||
* @param {Number | Complex | Unit} x
|
||||
* @return {Number | Complex} res
|
||||
*/
|
||||
function atan(x) {
|
||||
if (isNumber(x)) {
|
||||
return Math.atan(x);
|
||||
}
|
||||
|
||||
if (x instanceof Complex) {
|
||||
// atan(z) = 1/2 * i * (ln(1-iz) - ln(1+iz))
|
||||
var re = x.re;
|
||||
var im = x.im;
|
||||
var den = re * re + (1.0 - im) * (1.0 - im);
|
||||
|
||||
var temp1 = new Complex(
|
||||
(1.0 - im * im - re * re) / den,
|
||||
(-2.0 * re) / den
|
||||
);
|
||||
var temp2 = log(temp1);
|
||||
|
||||
return new Complex(
|
||||
-0.5 * temp2.im,
|
||||
0.5 * temp2.re
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: implement array support
|
||||
// TODO: implement matrix support
|
||||
|
||||
throw newUnsupportedTypeError('atan', x);
|
||||
}
|
||||
|
||||
math.atan = atan;
|
||||
|
||||
/**
|
||||
* Function documentation
|
||||
*/
|
||||
atan.doc = {
|
||||
'name': 'atan',
|
||||
'category': 'Trigonometry',
|
||||
'syntax': [
|
||||
'atan(x)'
|
||||
],
|
||||
'description': 'Compute the inverse tangent of a value in radians.',
|
||||
'examples': [
|
||||
'atan(0.5)',
|
||||
'atan(tan(2.3))'
|
||||
],
|
||||
'seealso': [
|
||||
'tan',
|
||||
'acos',
|
||||
'asin'
|
||||
]
|
||||
};
|
||||
@ -8,7 +8,7 @@ function cos(x) {
|
||||
return Math.cos(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
// cos(z) = (exp(iz) + exp(-iz)) / 2
|
||||
return new Complex(
|
||||
0.5 * Math.cos(x.re) * (Math.exp(-x.im) + Math.exp(x.im)),
|
||||
|
||||
@ -8,7 +8,7 @@ function sin(x) {
|
||||
return Math.sin(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
return new Complex(
|
||||
0.5 * Math.sin(x.re) * (Math.exp(-x.im) + Math.exp( x.im)),
|
||||
0.5 * Math.cos(x.re) * (Math.exp( x.im) - Math.exp(-x.im))
|
||||
|
||||
@ -8,7 +8,7 @@ function tan(x) {
|
||||
return Math.tan(x);
|
||||
}
|
||||
|
||||
if (isComplex(x)) {
|
||||
if (x instanceof Complex) {
|
||||
var den = Math.exp(-4.0 * x.im) +
|
||||
2.0 * Math.exp(-2.0 * x.im) * Math.cos(2.0 * x.re) +
|
||||
1.0;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<title>mathjs test</title>
|
||||
|
||||
<script src="../math.js" type="text/javascript"></script>
|
||||
<script src="../lib/math.js" type="text/javascript"></script>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user