# How does Indexed work in terms of sparse array

I want to use indexed given that s is an element [0,500] but I am unsure how to write that without getting a format error or a tensor error.

Δt = .0001;
t = .0833;
σ = .2183;
Δs = 5;
s = [0, 500];
μ = ((σ^2 Indexed[s, i]^2)/
Indexed[Δs, i]^2*Δt);
α = (Indexed[s, i]/(
2*Indexed[Δs, i]^2*Δt));
cn1[k2_, n_] =
SparseArray[{{m_, m_} ->
1/2 + 1/2*μ +
1/2*Indexed[rate, {k2, n}]*Δt, {m_, l_} /;
l - m == 1 -> -(1/4)*μ -
1/2*Indexed[rate, {k2, n}]*α, {m_, l_} /;
m - l == 1 -> -(1/4)*μ +
1/2*Indexed[rate, {k2, n}]*α}, {101, 101}]

• One of the many points is that you write Indexed[\[CapitalDelta]s, i] while \[CapitalDelta]s equals 5. That just does not make sense because you can index only into lists or arrays. Also s = [0, 500]; is meaningless in Mathematica. Maybe you mean s = {0, 500};. Also, I am quite sure that you should use cn1[k2_, n_] := ... (SetDelayed) instead of cn1[k2_, n_] = ... (Set). – Henrik Schumacher Dec 23 '19 at 8:33
• I have corrected your points, thank you! My first question is, after making those changes and commenting out the Delta S, there is no out. Although, in terms of the \[CapitalDelta]=5, the reason I did this is because when I derived my equations, I found some s_i and some \[CapitalDelta]s and I assumed for every S I needed to do it in terms of time hence why I added the delta in the question. You are saying it does not make sense because it is only single value which defeats the purpose of indexed? @HenrikSchumacher – Cherry Dec 23 '19 at 8:41
• "You are saying it does not make sense because it is only single value which defeats the purpose of indexed? " Yes. – Henrik Schumacher Dec 23 '19 at 8:51

I find the pattern-based way of using SparseArray not overly helpful in practice, but that's probably a matter of taste. However, you might find this way to construct the matrix easier. I am using just any constants for  Δt, μ, and α.

This is what you run once:

Δt = .0001;
μ = 1.;
α = 1.;

A = Plus[
DiagonalMatrix[SparseArray[ConstantArray[1/2 + μ/2, 101]]],
DiagonalMatrix[SparseArray[ConstantArray[ -μ/4, 100]], 1],
DiagonalMatrix[SparseArray[ConstantArray[ -μ/4, 100]], -1]
];

B = Plus[
DiagonalMatrix[SparseArray[ConstantArray[ Δt/2, 101]]],
DiagonalMatrix[SparseArray[ConstantArray[-α/2, 100]], 1],
DiagonalMatrix[SparseArray[ConstantArray[ α/2, 100]], -1]
];


And in each time step of Crank-Nicolson (I guess you are about to implement Crank-Nicolson, right), you obtain the matrix you seek for with

L = A + Indexed[rate, {k2, n}] B

• Ah yes I was having an issue combining my two sparse arrays! Thank you so much for this clarification! Although I did have one question, why is it that indexed stays in the sparse array you get as an output for L? I assumed that, yes, the output would be sectioned off into four parts: namely (1,2),(1,1),(2,1), and (2,3) but it does not combine lets say the 1/2+_ and rather just prints it out next to the indexed function @Henrik Schumacher – Cherry Dec 23 '19 at 9:06
• Also, since the code has changed wouldn't the k2 change as well? since it is no longer defined @HenrikSchumacher – Cherry Dec 23 '19 at 9:13
• Well, it is hard for me to help you here because I do not know why you introduced the parameters k2 and n in the first place. That's really something that you have to make your mind up about for yourself. – Henrik Schumacher Dec 23 '19 at 9:38
• "I assumed that, yes, the output would be sectioned off into four parts: namely (1,2),(1,1),(2,1), and (2,3) [...]" Sorry, I absolutely do not understand what you mean. "[...] rather just prints it out next to the indexed function [...]" Same problem here. But maybe your issue is simply that rate is still undefined and not a matrix? – Henrik Schumacher Dec 23 '19 at 9:39
• I introduced k2 and n because those were supposed to be independent variables that represent sequence number and time state, respectively. Also, I meant to say that for example one section of an output I got was (1,2)->-(1.19x10^-6Indexed[{0,5000},{i}]^2)/(Indexed[5,i]^2). Because the word indexed is printing out in my code, does that mean there is an issue with the iterative method of S_i . Also if I had questions on how to add my \[CapitalDelta]s should I make a new thread for that? @HenrikSchumacher – Cherry Dec 23 '19 at 16:50