5
$\begingroup$

I want to extract the coefficients from the following list:

list={16.2219 Sin[16.2219 t], 6.60924 Sin[6.60924 t], 13.927 Sin[
13.927 t], 2.91309 Sin[2.91309 t]}

The only way I've found it to work is if I put the exact expression for Sin in the following manner,

Coefficient[list[[1]], Sin[16.2219]]

My actual list is very long and each Sin has a different argument. In a perfect world I would like to do,

coefs = Table[Coefficient[list[[i]],Sin[t]],{i,1,Length[list]}]

Anyone have experience with this? Thanks!

$\endgroup$
2
  • 1
    $\begingroup$ Iterate over Variables[list]. $\endgroup$ Oct 16, 2014 at 17:50
  • $\begingroup$ Coefficient[#, #2] & @@@ Thread@{list, Variables /@ list}: i.stack.imgur.com/1zlFL.png $\endgroup$
    – Öskå
    Oct 16, 2014 at 17:51

5 Answers 5

6
$\begingroup$

A fairly general approach is to use pattern matching, but always look at the FullForm of mathematical expressions before you write your patterns or you may be surprised.

Cases[list, x_*_Sin :> x]
{16.2219, 6.60924, 13.927, 2.91309}

For the particular case you can also use:

list /. _Sin -> 1
{16.2219, 6.60924, 13.927, 2.91309}

Or simply extract the parts you want (which rhermans already showed):

First /@ list
{16.2219, 6.60924, 13.927, 2.91309}
$\endgroup$
6
$\begingroup$

This might not be the standard solution, but anyways. If all elements have exactly the form $a\sin(at)$ with $a>0$, you could do

Sqrt[D[list, t] /. t -> 0]
{16.2219, 6.60924, 13.927, 2.91309}

since $D(a\sin(at))=a^2\cos(at)$ which equals $a^2$ for $t=0$.

$\endgroup$
3
  • $\begingroup$ I gladly admit that the other solution(s) proposed is better than this. What is the standard then, should I delete it? $\endgroup$
    – mickep
    Oct 16, 2014 at 17:55
  • 3
    $\begingroup$ welcome to Mathematica.SE. I suggest you keep the answer. It is an interesting outside-the-box approach. (+1) $\endgroup$
    – kglr
    Oct 16, 2014 at 18:03
  • $\begingroup$ @kguler Thank you very much for your kind comment and information. $\endgroup$
    – mickep
    Oct 16, 2014 at 18:59
3
$\begingroup$

If you need to use Coefficient you can do

Coefficient[list /. Sin[_] :> z, z]
(* {16.2219,6.60924,13.927,2.91309} *)

If not, many alternatives, including

list /. Sin -> (1 &)
(* {16.2219,6.60924,13.927,2.91309} *)
$\endgroup$
3
$\begingroup$
#[[1]] & /@ list

also known as

First /@ list

(as pointed bt @Mr.Wizard).

Or (credit to @DanielLichtblau)

First@(#/Variables[#]) & /@ list
{16.2219, 6.60924, 13.927, 2.91309}
$\endgroup$
1
  • 2
    $\begingroup$ FYI: #[[1]] & /@ list would be better written as list[[All, 1]]. +1 nevertheless $\endgroup$
    – Mr.Wizard
    Oct 16, 2014 at 18:06
0
$\begingroup$

you can also try these:

list /. Sin[__] :> Sin[\[Pi]/2]

Level[list, {2}][[1 ;; -1 ;; 2]]

DeleteCases[list, Sin[__], -1]

Replace[list, _ -> \[Pi]/2, {3}]

(*{16.2219, 6.60924, 13.927, 2.91309}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.