Maybe I should apologize for this silly question, but the documentation for Indexed is very short and not explicit.

I have an equation with variables Indexed[x,n] and construct a sparse matrix through CoefficientArrays.

How can I choose all $x$ with an index value $n≥|b|$ as variables in CoefficientArrays?

And how can I set a rule of substitution for an indexed variable? Construction of form Indexed[x,1]=a^2 doesn't work, so what's the correct variant?

Thank you in advance for your help

  • $\begingroup$ I can't quite figure out what you are asking. It would help if you could give a small snippet of code that shows the kind of thing you have and what you expect to see as output. $\endgroup$ – bill s Jun 5 '15 at 7:48
  • $\begingroup$ You could index your variables using x[n]; you can freely assign values to such variables. $\endgroup$ – MarcoB Jun 5 '15 at 7:56
  • $\begingroup$ @bills code to the first question: eqnsList = {5*Indexed[x, 1] - f^2 Indexed[x, -3] + Sqrt[2] Indexed[x, 4] == 0, 1/5 Indexed[x, -4] - g Indexed[x, 2] == 0}; linsystem = CoefficientArrays[eqns, Indexed[x, 1]]. This code is good when I get only factors of Indexed[x, 1], but I don't know how to choose as variables all $x$ with an index factor $n\geq \left| 3\right|$ About the second one: if I want to substitute smth as a^2 in all expr. for Indexed[x,1], I do it like Indexed[x,1]=a^2, but I get only an error >Set::write: Tag Indexed in Subscript[coefA, 1] is Protected. >> $\endgroup$ – Hedin Jun 5 '15 at 9:15
  • $\begingroup$ @Hedin -- you should place your code in the question itself so that others can easily find it (you can use the "edit button" to edit your question). $\endgroup$ – bill s Jun 5 '15 at 9:42

I'm not sure I know what you are trying to accomplish, but you can do the same thing for any set of indices that you are doing for Index[x,1] using Map (shortcut /@) as follows:

eqnsList = {5*Indexed[x, 1] - f^2 Indexed[x, -3] + Sqrt[2] Indexed[x, 4] == 0, 
            1/5 Indexed[x, -4] - g Indexed[x, 2] == 0}; 
linsystem = CoefficientArrays[eqnsList, Indexed[x, #]] & /@ {-3, 1, 2, 4}

This does the same thing for indices {-3, 1, 2, 4}. Of course you can stick any set you wish in their place.

For the second question, you can use a replacement rule, for instance:

eqnsList /. Indexed[x, 1] -> a^2

replaces all occurrences of Indexed[x, 1] with a^2.

  • $\begingroup$ glad to help. I didn't feel easy on pure functions yet, but I'll try to fix that drawback. $\endgroup$ – Hedin Jun 5 '15 at 10:15

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.