@@ -4352,42 +4352,57 @@ if((typeof module) !== 'undefined') {
4352
4352
}
4353
4353
else if ( ( symbol . fname === 'cos' || symbol . fname === 'sin' ) &&
4354
4354
( 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 ) ;
4387
4369
}
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
+ }
4389
4403
} else if ( ( symbol . fname === 'cos' || symbol . fname === 'sin' ) &&
4390
4404
( symbol . args [ 0 ] . multiplier . sign ( ) == - 1 ) ) {
4405
+ // sin(-x) => -sin(x), cos(-x) => cos(x)
4391
4406
// remove the minus from the argument
4392
4407
const newArg = symbol . args [ 0 ] . clone ( ) . negate ( ) ;
4393
4408
// make the new trig call
@@ -4401,6 +4416,7 @@ if((typeof module) !== 'undefined') {
4401
4416
} if ( symbol . fname === 'sin' &&
4402
4417
symbol . args [ 0 ] . multiplier . equals ( 2 ) &&
4403
4418
! symbol . args [ 0 ] . equals ( 2 ) ) {
4419
+ // sin(2x) => 2sin(x)cos(x)
4404
4420
// remove the minus from the argument
4405
4421
const newArg = symbol . args [ 0 ] . clone ( ) . toUnitMultiplier ( ) ;
4406
4422
// make the new trig call
@@ -4410,8 +4426,6 @@ if((typeof module) !== 'undefined') {
4410
4426
workDone = true ;
4411
4427
}
4412
4428
4413
-
4414
-
4415
4429
retval = __ . Simplify . unstrip ( sym_array , retval ) . distributeMultiplier ( ) ;
4416
4430
symbol = retval ;
4417
4431
if ( iterations > 10 ) {
0 commit comments