Skip to content

Commit

Permalink
trig identities part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmein-ts committed Sep 20, 2024
1 parent cb91dfa commit 49e1e7b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
84 changes: 49 additions & 35 deletions Algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -4352,42 +4352,57 @@ if((typeof module) !== 'undefined') {
}
else if((symbol.fname === 'cos' || symbol.fname === 'sin') &&
(symbol.args[0].group == CP)) {

// capture cos(x-pi/2) => sin(x) and sin(x+pi/2) = cos(x)
// but generalized
// test the sum for presence of a "n*pi/2" summands
let count = 0;
let newArg = new Symbol(0);
let piOverTwo = _.parse('pi/2');
symbol.args[0].each((x)=>{
let c = _.divide(x.clone(), piOverTwo.clone());
c = __.Simplify._simplify(c);
c = core.Utils.evaluate(c);
if (isInt(c)) {
count += c.multiplier.num.value;
} else {
newArg = _.add(newArg, x);
}
});
if (count) {
count = count % 4;
count += (count < 0)?4:0;
count += (symbol.fname === 'cos')?1:0;
// console.log(count);
// debugger;
const results = [
"sin({0})",
"cos({0})",
"-sin({0})",
"-cos({0})",
];
const s = core.Utils.format(results[count], newArg);
retval = _.parse(s);
workDone = true;
// capture cos(x-pi/2) => sin(x) and sin(x+pi/2) = cos(x)
// but generalized
// test the sum for presence of a "n*pi/2" summands
let count = 0;
let newArg = new Symbol(0);
let piOverTwo = _.parse('pi/2');
symbol.args[0].each((x)=>{
let c = _.divide(x.clone(), piOverTwo.clone());
c = __.Simplify._simplify(c);
c = core.Utils.evaluate(c);
if (isInt(c)) {
count += c.multiplier.num.value;
} else {
newArg = _.add(newArg, x);
}

});
if (count) {
count += (symbol.fname === 'cos')?1:0;
count = count % 4;
count += (count < 0)?4:0;
// console.log(count);
// debugger;
const results = [
"sin({0})",
"cos({0})",
"-sin({0})",
"-cos({0})",
];
const s = core.Utils.format(results[count], newArg);
retval = _.parse(s);
workDone = true;
} else if (Object.keys(symbol.args[0].symbols).length > 1) {
// apply sin(a+-b) => sin(a)cos(b)+-cos(a)sin(b)
// and cos(a+-b) => cos(a)cos(b)-+sin(a)sin(b)
const arg = symbol.args[0].clone();
const summands = Object.values(arg.symbols);
const a = summands[0];
const b = summands.slice(1);
const bStr = b.map((x)=>"("+x.text()+")").join("+");
let s;
if (symbol.fname === 'sin') {
s = core.Utils.format("sin({0})cos({1})+sin({1})cos({0})", a, bStr);
} else {
s = core.Utils.format("cos({0})cos({1})-sin({1})sin({0})", a, bStr);
}
retval = _.parse(s);
workDone = true;
}
} else if((symbol.fname === 'cos' || symbol.fname === 'sin') &&
(symbol.args[0].multiplier.sign() == -1)) {
// sin(-x) => -sin(x), cos(-x) => cos(x)
// remove the minus from the argument
const newArg = symbol.args[0].clone().negate();
// make the new trig call
Expand All @@ -4401,6 +4416,7 @@ if((typeof module) !== 'undefined') {
} if(symbol.fname === 'sin' &&
symbol.args[0].multiplier.equals(2) &&
!symbol.args[0].equals(2)) {
// sin(2x) => 2sin(x)cos(x)
// remove the minus from the argument
const newArg = symbol.args[0].clone().toUnitMultiplier();
// make the new trig call
Expand All @@ -4410,8 +4426,6 @@ if((typeof module) !== 'undefined') {
workDone = true;
}



retval = __.Simplify.unstrip(sym_array, retval).distributeMultiplier();
symbol = retval;
if (iterations > 10) {
Expand Down
2 changes: 1 addition & 1 deletion all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "javascript light-weight symbolic math library",
"name": "nerdamer-prime",
"license": "MIT",
"version": "1.1.23",
"version": "1.1.24",
"homepage": "https://github.com/together-science/nerdamer-prime",
"directory": {
"lib": "./"
Expand Down
5 changes: 2 additions & 3 deletions spec/algebra.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,8 @@ describe('Algebra', function () {

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

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

it('regression tests', function() {
Expand Down
2 changes: 1 addition & 1 deletion spec/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ try {
// console.log(x.text());
// return;
// x= nerdamer("-sqrt(8/12)")
x= nerdamer("limit(tan(3*x)/tan(x), x, pi/2)");
x= nerdamer("simplify(cos(a+b))");
console.log(x.text());
} catch (error) {
console.log("error "+error)
Expand Down

0 comments on commit 49e1e7b

Please sign in to comment.