Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The simplest example of the change being sought is a greek letter, typed in as plaintext nu, and its may be replaced by the symbol, ν:

expr = 3nu*kx*ky+ expr /. nu -> ν yielding 3νkxky

However, I need to make a variable with a subscript correspond to a large number of different subscript values (many different possibilities for x in "k_x").

In any one of my (100+) equations, it's easy enough to change its couple of variables to their more visually comprehensible counterpart manually using the above method along with Subscript, but I am having trouble finding an efficient way to have Mathematica take ANY given kz and output the corresponding Subscript[k,z] output.

A method of achieving this here automatedly from Mathematica taking a glance at my plaintext system of equations would be great.

Thank you!

Edit: After talking with Bill S briefly in the comments, he suggested I upload an example of what I have so far and what I hope to accomplish. If arrays are a better way to handle a majority of my input, even in conjunction with subscripts at times, I am more than willing to use them. Here's an examplary 3 equations where R# signifies the rate of change of Z[#], where Z[#] on the RHS only coincidentally contains the function bracket notation due to use of other software.

R6 = kf*(z[1] + mu*z[5]) - 2*z[6] + mu*kd^2*z[7] - kd^2*z[6]
R7 = kf*(z[1] + mu*z[6] - 2*z[7]) + kd^2*z[8] + kd^2*z[7]
R8 = kf*(z[1] + (mu*z[7] - 2*z[8]) + kd^2*z[9] + kd^2*z[8]

As you can see, the number of options for x in k_x is only 2: f & d, so I can input the /. rules easily manually. However, to avoid confusion between my plaintext input form's z[x] and Mathematica's traditional single-function notation, I would like to change ALL z[x] to z_x (z with subscript x), regardless of specific x. However, here there are 140 different values rather than 2, so manually inputting each /. rule is less practical.

My current goal is to simplify the appearance and allow for symbolic manipulation of all the parameters, but I do hope to do curve-fitting eventually. Does anyone have advice on how to approach this scenario, either via subscripts or Array? I suspect # and possibly ToRules will be involved but am not sure yet how to do this.

Answered! Please see the answer and, if you're only interested in efficiently replacing any symbolic expression with another, see the comment on the answer.

share|improve this question
    
This is no doubt possible, for instance, kz //. kz -> Subscript[k, z], but this is the hard way. Instead of using subscripts, you should consider using arrays, which can be indexed into using Part. Subscript is primarily a display construct and if you try to use it for data manipulation, it makes everything more complicated. –  bill s May 11 '13 at 4:22
    
@bill I have managed to yield the subscripted component successfully for any one, my problem is in doing it for ANY of them without specifying each rule separately (which would be a list of 100+ rules for each time z changes). I'm going to be using it for manipulation of a large system of (100+) differential equations. There will be curve-fitting involved, varying the k_z variables to best fit. Does it sound as if subscripts are generally a bad idea? –  Ghersic May 11 '13 at 4:28
    
@bill To bolster the scientific respectability of varying multiple variables in a large system of single-variable ODE's, we DO have methods of acquiring decent approximations of each instatiation of k_z beforehand. So in short, time is the independant variable, whereas the k_z terms are coefficients dependant upon physical parameters that I would like to vary manually. (In clarification for my previous comment) –  Ghersic May 11 '13 at 4:32
    
I don't understand exactly what your inputs and desired outputs look like. But arrays are a much more easily manipulated data structure in Mathematica than are subscripts.What's wrong with an array k that contains hundreds of elements; whenever you need to access one, you use k[[24]] or k[[n]] where n can be any integer (or range of integers). –  bill s May 11 '13 at 4:49
    
Your mentioning arrays simplifies the more intimidating side of what I'm attempting to do, omitted from my question initially for the sake of simplicity. I'm much more interested in applying your array concept to 140 functions of the form z[n] where z[140] is described in terms of z[139] and so forth. Given previous use of other software, I have them in the form of z[n-1] as a function of z[n] in plaintext already, with slight variations from z[n] to z[n-1]. In your opinion, is combining the Arrayfunction with z[n] = k_x*z[n-1] and z[n-1] = k_y*z[n-2], etc. plausible? –  Ghersic May 11 '13 at 5:03
show 7 more comments

2 Answers

up vote 2 down vote accepted

If this really can be expressed linearly, then expressing in Matrix form is going to be the easiest thing and the best from a computational perspective. For example, with your definitions:

R6 = kf*(z[1] + mu*z[5]) - 2*z[6] + mu*kd^2*z[7] - kd^2*z[6];
R7 = kf*(z[1] + mu*z[6] - 2*z[7]) + kd^2*z[8] + kd^2*z[7];
R8 = kf*z[1] + (mu*z[7] - 2*z[8]) + kd^2*z[9] + kd^2*z[8];

You can find the matrix using Coefficient

a = {Table[Coefficient[R6, z[i]], {i, 1, 9}], Table[Coefficient[R7, z[i]], {i, 1, 9}], Table[Coefficient[R8, z[i]], {i, 1, 9}]}

which is, in nicer form:

enter image description here

Now you can verify that

FullSimplify[a.{z[1],z[2],z[3],z[4],z[5],z[6],z[7],z[8],z[9]} - {R6,R7,R8}]

is zero (0,0,0). Assuming you have a lot of these equations, you could build a table of Tables rather than specifying each row individually. And in fact, if your matrix has a nice banded structure (as it appears to from the small piece) then you can just define it in Mathematica and skip the complexities of importing a text file. (There are a number of nice functions for specifying sparse and banded matrices).

share|improve this answer
    
Thank you, I'll the look into both Array and the coefficient matrix method you suggest here. At first, I was dismayed that someone might come here hoping to simply make all their characters subscripts instead of (whatever), but I actually just figured that out as well. Lord, I don't know why I didn't see this sitting in front of my face from the documentation: (after dealing with the easy k_f, k_d, terms from the example we have) expr /. z[p_] -> Subscript[z, p] Where it does work despite the sinister similarity in appearance with function notation brackets in Mathematica. @bill –  Ghersic May 12 '13 at 3:18
add comment

Does this do the job?

poly=(kz py + py + kr kx)
variables = Variables[poly];
reprule =DeleteCases[If[StringTake[ToString[#], 1] == "k", # -> 
  Subscript[StringTake[ToString[#], 1], 
   StringTake[ToString[#], {2, -1}]]] & /@ variables, Null]
poly/.reprule

This will take all the variables in an expression, work out if they are a combination of k and another letter and if they are, it will convert them to k with a subscript of the letter you are interested in.

share|improve this answer
    
Thank you, I think it does; this will be useful down the road when I'm dealing with more than two unique subscripted k's. Sorry to have not noticed your answer until now. –  Ghersic May 13 '13 at 14:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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