# Updating the association with answers from the TrigProperties Function

I have created a list of rules I would like to update with values. My goal here is to be able to write the following:

A*sin(B*x+C)+D (*Function given*)
<|"Amplitude" ->__,"Period"-> __,"PhaseShift"->__,"Displacement" -> __|> (*Desired Outcome*)


I cannot seem to get the result with what I have and I am not sure what I am doing wrong. My goal was to associate the names with the variables, and the variables with the functions so that when I run the code with different values for A,B,C,and D, the association is updated with the values. I also coded in a rule that the function should fail to evaluate if A = 0, and B = 0:

SetAttributes[rules, HoldAll]
rules = Association[{Amp -> "Amplitude", Pd -> "Period",
Ph -> "Phase", Dis -> "Displacement"}];
Amp [a_Integer] := Abs[a];
Pd[b_Integer] := 2 \[Pi]/b;
Ph[c_Integer] := -C/B;
Dis[d_Integer] := MatchQ[d, _Integer];

f[x_] := FullForm[Amp*Sin[Pd*x + Ph] + D];
TrigProperties[f[x_]] :=
While[Pd > 0, If[b == 0, Break[]]; Print["Failed"]];
If[Amp == 0, Break[]]; Print["Failed"];
f[x] /@ rules;
TrigProperties[]


How can I properly map the values I put into TrigProperties into the above association "rules"?

UPDATE: Impelemented the changes in the comments. Code is still not working and throwing the following errors:

RecursionLimit: Recursion depth of 1024 exceeded during evaluation of Plus[Times[Amp,Sin[Plus[Times[Pd,x],Ph]]],Dis].
RecursionLimit: Recursion depth of 1024 exceeded during evaluation of TrigProperties[f[x]] := While[Pd>0,If[Pd==0,Break[]];Print[Failed]]
RecursionLimit: Recursion depth of 1024 exceeded during evaluation of I[Amp==0,Break[]]

• Unless I misunderstand the programming style you are attempting here, there are quite a few issues with your workflow in terms of defining a function to do what you want it to do. Why not define the function as a Module? You’ll need to wrap your input in the function, typically, which you do not seem to be doing in the example you’re giving. Also, are these vectors you’re using for your variable terms? If not, why are you trying to use a dot product between them? There are a lot of syntactical things that can and should be improved about this. I don’t have your answer, but someone else will. Jan 16, 2022 at 8:36
• I think you want something like this for your input expression: amp*Sin[pd*x + ph] + disp. Compare the FullForm of this to the one for your expression to see how they parse differently. Jan 17, 2022 at 21:15
• @DanielLichtblau I implemented this change and it seems to eliminate the errors I had on the parentheses Jan 18, 2022 at 3:46
• @CATrevillian, I had trouble with the Module wrapper early on but I "fixed" that issue. Now it seems to print out RecursionLimit errors at each step past the module. Jan 18, 2022 at 3:56
• If you can update your post with the code that is still not working, that would be very helpful. As it is, it is still difficult to pinpoint how to help you with this. Thanks! Jan 18, 2022 at 4:15

You might want to tackle this using a pattern matching approach. The code below simply tries to match a pattern of the form you indicate to be of interest. It allows for default values for the Plus and Times operators. Possibly the use of a frequency is not what you have in mind for getting the period so that may need tweaking.

trigProperties[amp_.*(Sin | Cos)[freq_.*x_ + phase_.] + disp_.,
x_] := <|"Amplitude" -> amp, "Period" -> 2*Pi/freq,
"PhaseShift" -> phase, "Displacement" -> disp|>


Here is how it handles the original example.

In[2766]:= trigProperties[amp*Sin [w*x + pd] + disp, x]

(* Out[2766]= <|"Amplitude" -> amp,
"Period" -> (2 \[Pi])/w,
"PhaseShift" -> pd,
"Displacement" -> disp|> *)


Here is a case where some of the parameters are not present, so the defaults get filled in.

In[2767]:= trigProperties[Cos [3*x] + 2, x]

(* Out[2767]= <|"Amplitude" -> 1,
"Period" -> (2 \[Pi])/3,
"PhaseShift" -> 0, "Displacement" -> 2|> *)