2
$\begingroup$

First of all, this is quite related to this question here, however the solution given there does not apply to my issue.

I am generating a huge symbolic matrix, which has many (on the order of 50-100) indexed parameters originating in various sums. All of my symbols and indexed variables are real, so I would like to make use of that when I for instance perform a Conjugate operation on the matrix.

See below minimal working example:

test = Exp[I*k[1]];
# + Conjugate[#] &@test
(* E^(-I Conjugate[k[1]]) + E^(I k[1]) *)
Refine[%, Assumptions -> k \[Element] Reals]
(* E^(-I Conjugate[k[1]]) + E^(I k[1]) *)
Refine[%, Assumptions -> k[1] \[Element] Reals]
(* E^(-I k[1]) + E^(I k[1])1 *)

An approach like Refine[...,Assumptions->_Symbol ∈ Reals] will also not work, since Head[k[1]] is k, not Symbol. The list of all indexed variables is not known in advance and changes as soon as I play with certain parameters, so I cannot just retrieve them once in order to get a full list and use this in Assumptions.

Is there a way to tell mathematica that all Symbols and indexed variables are Reals? Presumably it can be done using Symbolize from the Notation package, but I don't know how (I have seen it for Subscripts but really want to avoid using them). How to deal with symbols is clear from the linked question, but achieving it for indexed variables is a myth for me right now.

$\endgroup$
  • $\begingroup$ Can you use ComplexExpand? $\endgroup$ – Marius Ladegård Meyer Nov 22 '16 at 11:07
  • $\begingroup$ @MariusLadegårdMeyer Right now, yes. But the size of my matrices will grow quite a lot in future work and also the "length" of each element. So I am unsure if ComplexExpand might take too long in those cases. I was thinking about using a rule like Complex[a_,b_]:>Complex[a,-b] instead of Conjugate but I have a feeling that this is not safe. $\endgroup$ – Lukas Nov 22 '16 at 12:00
  • $\begingroup$ What are you going to do with the matrices? Look at them, solve equations involving them...? $\endgroup$ – Marius Ladegård Meyer Nov 22 '16 at 12:18
  • $\begingroup$ @MariusLadegårdMeyer At first, it is mostly looking at them and transfering them to Matlab for further numerical simulations. However, depending on the numerical results, I might also try to work with the symbolic matrices in Mathematica which would for instance involve some series expansions and solving equations based on those matrices. $\endgroup$ – Lukas Nov 22 '16 at 12:21
  • 2
    $\begingroup$ Assumptions -> _k ∈ Reals, or Assumptions -> (s_Symbol /; Context@s =!= "System`")[_Integer? Positive] ∈ Reals? $\endgroup$ – jkuczm Nov 29 '16 at 17:17
1
$\begingroup$

An approach like Refine[...,Assumptions->_Symbol ∈ Reals] will also not work, since Head[k[1]] is k, not Symbol.

You are almost there: just use a proper pattern to match objects with Head k. Here are several ways:

test = Exp[I*k[1]];
expr = # + Conjugate[#] &@test;

Refine[expr, Assumptions -> _k ∈ Reals]
Refine[expr, Assumptions -> k[_] ∈ Reals]
Refine[expr, Assumptions -> k[_Integer] ∈ Reals]
E^(-I k[1]) + E^(I k[1])

E^(-I k[1]) + E^(I k[1])

E^(-I k[1]) + E^(I k[1])

(checked with Mathematica versions 11.2.0, 8.0.4 and 5.2).


Is there a way to tell Mathematica that all Symbols and indexed variables are Reals?

Here is a way to declare that every Symbol or indexed object is Real:

Refine[expr, Assumptions -> _Symbol[_Integer] ∈ Reals && _Symbol ∈ Reals]
E^(-I k[1]) + E^(I k[1])

A more restrictive approach to declare that every indexed variable is Real is suggested by jkuczm in the comments:

Refine[expr, 
 Assumptions -> (s_Symbol /; Context@s =!= "System`")[_Integer?Positive] ∈ Reals]
E^(-I k[1]) + E^(I k[1])

It can easily be extended to Symbols:

Refine[expr, 
 Assumptions -> ((s_Symbol[_Integer?Positive] | s_Symbol) /; 
     Context@s =!= "System`") ∈ Reals]
E^(-I k[1]) + E^(I k[1])
$\endgroup$
0
$\begingroup$

You can Symbolize classes of expressions based on rules. For example,

Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]], WorkingForm -> StandardForm]

will declare any subscripted expression to be considered a symbol and be subsequently treated as such. Also see the tutorial here.

Edit: Sorry, I realize I may have misunderstood your question. Do you want expressions such as k[1] to be considered symbols? If so, then I don't think that can be done. You may be able to construct some notation for certain types of expressions, but not in the way it appears from your original post, since that would require modifying core WL syntax.

$\endgroup$
  • $\begingroup$ Exactly, my idea for a solution was to symbolize k[1]. However, this is not mandatory... I thought it might be the easiest way to aim at declaring everything as real. $\endgroup$ – Lukas Nov 22 '16 at 17:40

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.