Skip to content

Commit 49e1e7b

Browse files
committed
trig identities part 2
1 parent cb91dfa commit 49e1e7b

File tree

5 files changed

+54
-41
lines changed

5 files changed

+54
-41
lines changed

Algebra.js

+49-35
Original file line numberDiff line numberDiff line change
@@ -4352,42 +4352,57 @@ if((typeof module) !== 'undefined') {
43524352
}
43534353
else if((symbol.fname === 'cos' || symbol.fname === 'sin') &&
43544354
(symbol.args[0].group == CP)) {
4355-
4356-
// capture cos(x-pi/2) => sin(x) and sin(x+pi/2) = cos(x)
4357-
// but generalized
4358-
// test the sum for presence of a "n*pi/2" summands
4359-
let count = 0;
4360-
let newArg = new Symbol(0);
4361-
let piOverTwo = _.parse('pi/2');
4362-
symbol.args[0].each((x)=>{
4363-
let c = _.divide(x.clone(), piOverTwo.clone());
4364-
c = __.Simplify._simplify(c);
4365-
c = core.Utils.evaluate(c);
4366-
if (isInt(c)) {
4367-
count += c.multiplier.num.value;
4368-
} else {
4369-
newArg = _.add(newArg, x);
4370-
}
4371-
});
4372-
if (count) {
4373-
count = count % 4;
4374-
count += (count < 0)?4:0;
4375-
count += (symbol.fname === 'cos')?1:0;
4376-
// console.log(count);
4377-
// debugger;
4378-
const results = [
4379-
"sin({0})",
4380-
"cos({0})",
4381-
"-sin({0})",
4382-
"-cos({0})",
4383-
];
4384-
const s = core.Utils.format(results[count], newArg);
4385-
retval = _.parse(s);
4386-
workDone = true;
4355+
// capture cos(x-pi/2) => sin(x) and sin(x+pi/2) = cos(x)
4356+
// but generalized
4357+
// test the sum for presence of a "n*pi/2" summands
4358+
let count = 0;
4359+
let newArg = new Symbol(0);
4360+
let piOverTwo = _.parse('pi/2');
4361+
symbol.args[0].each((x)=>{
4362+
let c = _.divide(x.clone(), piOverTwo.clone());
4363+
c = __.Simplify._simplify(c);
4364+
c = core.Utils.evaluate(c);
4365+
if (isInt(c)) {
4366+
count += c.multiplier.num.value;
4367+
} else {
4368+
newArg = _.add(newArg, x);
43874369
}
4388-
4370+
});
4371+
if (count) {
4372+
count += (symbol.fname === 'cos')?1:0;
4373+
count = count % 4;
4374+
count += (count < 0)?4:0;
4375+
// console.log(count);
4376+
// debugger;
4377+
const results = [
4378+
"sin({0})",
4379+
"cos({0})",
4380+
"-sin({0})",
4381+
"-cos({0})",
4382+
];
4383+
const s = core.Utils.format(results[count], newArg);
4384+
retval = _.parse(s);
4385+
workDone = true;
4386+
} else if (Object.keys(symbol.args[0].symbols).length > 1) {
4387+
// apply sin(a+-b) => sin(a)cos(b)+-cos(a)sin(b)
4388+
// and cos(a+-b) => cos(a)cos(b)-+sin(a)sin(b)
4389+
const arg = symbol.args[0].clone();
4390+
const summands = Object.values(arg.symbols);
4391+
const a = summands[0];
4392+
const b = summands.slice(1);
4393+
const bStr = b.map((x)=>"("+x.text()+")").join("+");
4394+
let s;
4395+
if (symbol.fname === 'sin') {
4396+
s = core.Utils.format("sin({0})cos({1})+sin({1})cos({0})", a, bStr);
4397+
} else {
4398+
s = core.Utils.format("cos({0})cos({1})-sin({1})sin({0})", a, bStr);
4399+
}
4400+
retval = _.parse(s);
4401+
workDone = true;
4402+
}
43894403
} else if((symbol.fname === 'cos' || symbol.fname === 'sin') &&
43904404
(symbol.args[0].multiplier.sign() == -1)) {
4405+
// sin(-x) => -sin(x), cos(-x) => cos(x)
43914406
// remove the minus from the argument
43924407
const newArg = symbol.args[0].clone().negate();
43934408
// make the new trig call
@@ -4401,6 +4416,7 @@ if((typeof module) !== 'undefined') {
44014416
} if(symbol.fname === 'sin' &&
44024417
symbol.args[0].multiplier.equals(2) &&
44034418
!symbol.args[0].equals(2)) {
4419+
// sin(2x) => 2sin(x)cos(x)
44044420
// remove the minus from the argument
44054421
const newArg = symbol.args[0].clone().toUnitMultiplier();
44064422
// make the new trig call
@@ -4410,8 +4426,6 @@ if((typeof module) !== 'undefined') {
44104426
workDone = true;
44114427
}
44124428

4413-
4414-
44154429
retval = __.Simplify.unstrip(sym_array, retval).distributeMultiplier();
44164430
symbol = retval;
44174431
if (iterations > 10) {

all.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description": "javascript light-weight symbolic math library",
77
"name": "nerdamer-prime",
88
"license": "MIT",
9-
"version": "1.1.23",
9+
"version": "1.1.24",
1010
"homepage": "https://github.com/together-science/nerdamer-prime",
1111
"directory": {
1212
"lib": "./"

spec/algebra.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,8 @@ describe('Algebra', function () {
369369

370370
expect(nerdamer('simplify(tan(x)-sin(x)/cos(x))').toString()).toEqual('0')
371371

372-
// todo:
373-
// expect(nerdamer('simplify(sin(x+y)-(sin(x)*cos(y)+cos(x)*sin(y)))').toString()).toEqual('0')
374-
// expect(nerdamer('simplify(cos(x+y)-(cos(x)*cos(y)-sin(x)*sin(y)))').toString()).toEqual('0')
372+
expect(nerdamer('simplify(sin(x+y)-(sin(x)*cos(y)+cos(x)*sin(y)))').toString()).toEqual('0')
373+
expect(nerdamer('simplify(cos(x+y)-(cos(x)*cos(y)-sin(x)*sin(y)))').toString()).toEqual('0')
375374
});
376375

377376
it('regression tests', function() {

spec/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ try {
4444
// console.log(x.text());
4545
// return;
4646
// x= nerdamer("-sqrt(8/12)")
47-
x= nerdamer("limit(tan(3*x)/tan(x), x, pi/2)");
47+
x= nerdamer("simplify(cos(a+b))");
4848
console.log(x.text());
4949
} catch (error) {
5050
console.log("error "+error)

0 commit comments

Comments
 (0)