3
$\begingroup$

I am struggling to put an argument in SparseArray. Let's consider the following example

dim = 9;
s = SparseArray[{{i_, i_} :> 1.0 + I, Band[{2, 1}] -> 2.0, 
   Band[{1, 2}] -> 2.0}, {dim, dim}, 0.0]

This works correctly and generates a matrix 9x9 with two diagonals having the value 2.0. Now I want to change some values on one of those diagonals, or better said, when I define s I would like to have a different value of 2.0 but only on some certain positions of that diagonal. For that I use an array lt specifying those positions in s, lt={3,6};, because one can specify manually a value for a certain position (for example {1,1}->4.0) and I tried to put the following Row[Flatten[Table[{i,i+1}->0.0,{i,lt}],1],","], having

s = SparseArray[{{i_, i_} :> 1.0 + I,Row[Flatten[Table[{i, i + 1} -> 0.0, {i, lt}], 1], ","], Band[{2, 1}] -> 2.0, 
   Band[{1, 2}] -> 2.0}, {dim, 
   dim}, 0.0]

but it failed, but If I put manually the positions

s = SparseArray[{{i_, i_} :> 1.0 + I,{3,4}->0.0,{6,7}->0.0, Band[{2, 1}] -> 2.0, 
   Band[{1, 2}] -> 2.0}, {dim, 
   dim}, 0.0]

it works. Of course this example is small and I can write it manually, but in real nb dimensions are bigger, that is why I use a Table command. Maybe the question could be also seen as how to obtain the output of Table without {}. Any advice is welcome.

$\endgroup$
4
  • $\begingroup$ I think instead of Row you want Sequence: SparseArray[{{i_, i_} :> 1.0 + I, Sequence @@ Flatten[Table[{i, i + 1} -> 0.0, {i, lt}], 1], Band[{2, 1}] -> 2.0, Band[{1, 2}] -> 2.0}, {dim, dim}, 0.0] -- Flatten may be omitted; it does nothing in this case. $\endgroup$
    – Michael E2
    Apr 30, 2021 at 0:26
  • $\begingroup$ Thanks a lot @MichaelE2 for your comment. Your line code does the job well. You could put it as an answer. I noticed that you use Sequence and watching this question [mathematica.stackexchange.com/questions/126755/… I think they are related $\endgroup$
    – DGtrauko
    Apr 30, 2021 at 1:49
  • $\begingroup$ Yep, Sequence does the same thing in that one. It is basically for threading arguments into slots, and Sequence @@ list will insert a list into a sequence of slots (that is, function arguments). $\endgroup$
    – Michael E2
    Apr 30, 2021 at 2:44
  • $\begingroup$ To add to the comment by @MichaelE2 : Row should never be used as a programming construct. It's meant purely for typesetting output. $\endgroup$ Apr 30, 2021 at 7:44

1 Answer 1

5
$\begingroup$

Here are two ways:

dim = 7;
lt = 2;
s = SparseArray[{{i_, i_} :> 1.0 + I, 
    Sequence @@ Table[{i, i + 1} -> 0.0, {i, lt}], 
    Band[{2, 1}] -> 2.0, Band[{1, 2}] -> 2.0}, {dim, dim}, 0.0];

s // MatrixForm
s2 = SparseArray[{{i_, i_} :> 1.0 + I, Band[{2, 1}] -> 2.0, 
    Band[{lt + 1, lt + 2}] -> 2.0}, {dim, dim}, 0.0];

s == s2
(*  True  *)
$\endgroup$

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.