6
$\begingroup$

I have a discrete function of an integer variable $n$ defined like this :

theta[n_] := 
 {0.0902266, 0.0889051, 0.0869336, 0.0840726, 0.0838656, 
  0.0931356, 0.0958106, 0.0931356, 0.0838666, 0.0840726, 0.0869326, 
 -0.0889051, 0.0902266, 0.0911066, 0.0917297, 0.0922456, 0.0927188, 
  0.0930936, 0.0932416, 0.0930936, -0.0927186, 0.0922446, 0.0917296, 
  0.0911066, 0.0869336, 0.0911066, 0.0931356, 0.0869336, -0.0931356,  
  0.0911066, -0.0872142, 0.0872142, 0.001, 0.0911066, 0.0872142,
  0.0911066, 0.001, 0.0911066, 0.00315, -0.00315}[[n]]

This function is defined so that I get these outputs :

theta[1] = 0.0902266
theta[2] = 0.0889051
...
theta[40] = -0.00315

I need to define a constant which is the total number of elements in the previous list. In the example above, there are $40$ elements inside the list, so that constant should be equal to $40$.

Apparently, I can't apply the command Lenght on the function above, in Mathematica. So how can I count the total number of outputs from that function ?

The reason I need this is that I may have to change the number of items inside that discrete function.

Any idea ?

$\endgroup$
6
$\begingroup$

You can take advantage of how the rules associated with theta are stored behind the scenes by doing

Length @ DownValues[theta][[1, 2, 1]]
(* 40 *)

This isn't robust, however. In the sense that if you have defined beforehand theta[10] = 5, for instance, then it won't find the right DownValue.

Alternatively, you can build the checking into the function:

Clear[theta, thetaHeld]
theta[n_?IntegerQ /; 1 <= n <= Length@thetaHeld[n][[1, 1]]] := ReleaseHold[thetaHeld[n]]
thetaHeld[n_] := Hold[
  List[0.0902266, 0.0889051, 0.0869336, 0.0840726, 0.0838656
   , 0.0931356, 0.0958106, 0.0931356, 0.0838666, 0.0840726
   , 0.0869326, -0.0889051, 0.0902266, 0.0911066, 0.0917297
   , 0.0922456,0.0927188, 0.0930936, 0.0932416, 0.0930936
   , -0.0927186, 0.0922446, 0.0917296, 0.0911066, 0.0869336
   , 0.0911066, 0.0931356, 0.0869336, -0.0931356, 0.0911066
   , -0.0872142, 0.0872142, 0.001, 0.0911066, 0.0872142
   , 0.0911066, 0.001, 0.0911066, 0.00315, -0.00315][[n]]]

This even works if the list has been defined before:

list = {0.0902266, 0.0889051, 0.0869336, 0.0840726, 0.0838656
   , 0.0931356, 0.0958106, 0.0931356, 0.0838666, 0.0840726
   , 0.0869326, -0.0889051, 0.0902266, 0.0911066, 0.0917297
   , 0.0922456,0.0927188, 0.0930936, 0.0932416, 0.0930936
   , -0.0927186, 0.0922446, 0.0917296, 0.0911066, 0.0869336
   , 0.0911066, 0.0931356, 0.0869336, -0.0931356, 0.0911066
   , -0.0872142, 0.0872142, 0.001, 0.0911066, 0.0872142
   , 0.0911066, 0.001, 0.0911066, 0.00315, -0.00315};


Clear[theta, thetaHeld]
theta[n_?IntegerQ /; 1 <= n <= Length@thetaHeld[n][[1, 1]]] := ReleaseHold[thetaHeld[n]]
thetaHeld[n_] := Hold[list[[n]]]

Then:

theta[4]
(* 0.0840726 *)

and

theta[41]
(* *)
$\endgroup$
2
$\begingroup$

Oh well, the solution to my problem is trivial. I simply have to change the definition of my theta function, like this :

Angles := {0.0902266, 0.0889051, 0.0869336, 0.0840726, 0.0838656, ...}

theta[n_] := Angles[[n]]

Then I can use the length command on the Angles :

Nangles = Length[Angles];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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