2
$\begingroup$

I'm using a subscript as a counter in some code. Here, I give a value:

Subscript[θ, 1] = 0

Without making any other assignments, I want to check if other Subscript[θ, i] have values.

Table[Subscript[θ, i], {i, 3}]
Table[ValueQ[Subscript[θ, i]], {i, 3}]

This give {0, Subscript[θ, 2], Subscript[θ, 3]} and {True,True,True}. Why isn't the second output {True,False,False}?

$\endgroup$
1
  • 2
    $\begingroup$ It's because Subscript[θ, i] evaluates to Subscript[θ, 2] and so it has a value. If you were to use Table[With[{i = i}, ValueQ[Subscript[θ, i]]], {i, 3}] you would get your desired result. $\endgroup$
    – Carl Woll
    Apr 3 at 0:17

1 Answer 1

4
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

Use indexed variables displayed as subscripted variables.

Format[θ[i_]] := Subscript[θ, i]

θ[1] = 0;

Table[θ[i], {i, 3}]

(* {0, Subscript[θ, 2], Subscript[θ, 3]} *)

If a value has been assigned then the Head is not θ

Table[Head[θ[i]] =!= θ, {i, 3}]

(* {True, False, False} *)

Or, to test for a numeric value

Table[NumericQ[θ[i]], {i, 3}]

(* {True, False, False} *)
$\endgroup$
1
  • $\begingroup$ NumericQ does it. I was hoping that the documentation on ValueQ would contain something like this in the See Also menu, but crickets. $\endgroup$ Apr 3 at 15:35

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.