Delete zeros from a sparse array [duplicate]












4















This question already has an answer here:




  • How to prevent a SparseArray entry from being specified when it coincides with the default value?

    1 answer




Why doesn't SparseArray automatically remove zero entries? The code



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0]
r //ArrayRules


returns




{{1}->-11,{3}->0,{5}->0,{7}->0,{9}->-99,{11}->0,{2}->22,{13}->1313,{_}->0}




instead of




{{1}->-11,{9}->-99,{2}->22,{13}->1313,{_}->0}.




Is there a nice way to get rid of zero entries? For instance, when I perform Gaussian elimination on $A!in!mathbb{Z}^{mtimes n}_2$, I use A = Mod[A, 2];, and suddenly many entries are zero. I don't want to waste memory on them.



Of course I could use



A = SparseArray[DeleteCases[ArrayRules[A], #[[2]] == 0&], {m,n}]


but it seems clumsy to use this often.



Also, when I have a very long sparse row that gets updated frequently and fills up somewhat (Gaussian elimination), is it better to use SparseArray with + or Association with Merge?










share|improve this question















marked as duplicate by Henrik Schumacher, Lukas Lang, Niki Estner, LCarvalho, anderstood 12 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
    – Szabolcs
    Dec 30 '18 at 9:11
















4















This question already has an answer here:




  • How to prevent a SparseArray entry from being specified when it coincides with the default value?

    1 answer




Why doesn't SparseArray automatically remove zero entries? The code



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0]
r //ArrayRules


returns




{{1}->-11,{3}->0,{5}->0,{7}->0,{9}->-99,{11}->0,{2}->22,{13}->1313,{_}->0}




instead of




{{1}->-11,{9}->-99,{2}->22,{13}->1313,{_}->0}.




Is there a nice way to get rid of zero entries? For instance, when I perform Gaussian elimination on $A!in!mathbb{Z}^{mtimes n}_2$, I use A = Mod[A, 2];, and suddenly many entries are zero. I don't want to waste memory on them.



Of course I could use



A = SparseArray[DeleteCases[ArrayRules[A], #[[2]] == 0&], {m,n}]


but it seems clumsy to use this often.



Also, when I have a very long sparse row that gets updated frequently and fills up somewhat (Gaussian elimination), is it better to use SparseArray with + or Association with Merge?










share|improve this question















marked as duplicate by Henrik Schumacher, Lukas Lang, Niki Estner, LCarvalho, anderstood 12 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
    – Szabolcs
    Dec 30 '18 at 9:11














4












4








4








This question already has an answer here:




  • How to prevent a SparseArray entry from being specified when it coincides with the default value?

    1 answer




Why doesn't SparseArray automatically remove zero entries? The code



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0]
r //ArrayRules


returns




{{1}->-11,{3}->0,{5}->0,{7}->0,{9}->-99,{11}->0,{2}->22,{13}->1313,{_}->0}




instead of




{{1}->-11,{9}->-99,{2}->22,{13}->1313,{_}->0}.




Is there a nice way to get rid of zero entries? For instance, when I perform Gaussian elimination on $A!in!mathbb{Z}^{mtimes n}_2$, I use A = Mod[A, 2];, and suddenly many entries are zero. I don't want to waste memory on them.



Of course I could use



A = SparseArray[DeleteCases[ArrayRules[A], #[[2]] == 0&], {m,n}]


but it seems clumsy to use this often.



Also, when I have a very long sparse row that gets updated frequently and fills up somewhat (Gaussian elimination), is it better to use SparseArray with + or Association with Merge?










share|improve this question
















This question already has an answer here:




  • How to prevent a SparseArray entry from being specified when it coincides with the default value?

    1 answer




Why doesn't SparseArray automatically remove zero entries? The code



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0]
r //ArrayRules


returns




{{1}->-11,{3}->0,{5}->0,{7}->0,{9}->-99,{11}->0,{2}->22,{13}->1313,{_}->0}




instead of




{{1}->-11,{9}->-99,{2}->22,{13}->1313,{_}->0}.




Is there a nice way to get rid of zero entries? For instance, when I perform Gaussian elimination on $A!in!mathbb{Z}^{mtimes n}_2$, I use A = Mod[A, 2];, and suddenly many entries are zero. I don't want to waste memory on them.



Of course I could use



A = SparseArray[DeleteCases[ArrayRules[A], #[[2]] == 0&], {m,n}]


but it seems clumsy to use this often.



Also, when I have a very long sparse row that gets updated frequently and fills up somewhat (Gaussian elimination), is it better to use SparseArray with + or Association with Merge?





This question already has an answer here:




  • How to prevent a SparseArray entry from being specified when it coincides with the default value?

    1 answer








matrix associations sparse-arrays rule






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 16:51









m_goldberg

84.3k872195




84.3k872195










asked Dec 30 '18 at 2:44









Leon

371111




371111




marked as duplicate by Henrik Schumacher, Lukas Lang, Niki Estner, LCarvalho, anderstood 12 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Henrik Schumacher, Lukas Lang, Niki Estner, LCarvalho, anderstood 12 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3




    It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
    – Szabolcs
    Dec 30 '18 at 9:11














  • 3




    It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
    – Szabolcs
    Dec 30 '18 at 9:11








3




3




It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
– Szabolcs
Dec 30 '18 at 9:11




It does not do it automatically because rebuilding a sparse array is expensive. It is left to you to decide when you want to rebuild it and eliminate unnecessary entries. Just apply SparseArray again.
– Szabolcs
Dec 30 '18 at 9:11










1 Answer
1






active

oldest

votes


















8














You can just apply SparseArray to a SparseArray object to normalize it. For your example:



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0];

SparseArray[r] //ArrayRules



{{1} -> -11, {2} -> 22, {9} -> -99, {13} -> 1313, {_} -> 0}







share|improve this answer





















  • For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
    – Leon
    Dec 30 '18 at 22:34


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














You can just apply SparseArray to a SparseArray object to normalize it. For your example:



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0];

SparseArray[r] //ArrayRules



{{1} -> -11, {2} -> 22, {9} -> -99, {13} -> 1313, {_} -> 0}







share|improve this answer





















  • For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
    – Leon
    Dec 30 '18 at 22:34
















8














You can just apply SparseArray to a SparseArray object to normalize it. For your example:



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0];

SparseArray[r] //ArrayRules



{{1} -> -11, {2} -> 22, {9} -> -99, {13} -> 1313, {_} -> 0}







share|improve this answer





















  • For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
    – Leon
    Dec 30 '18 at 22:34














8












8








8






You can just apply SparseArray to a SparseArray object to normalize it. For your example:



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0];

SparseArray[r] //ArrayRules



{{1} -> -11, {2} -> 22, {9} -> -99, {13} -> 1313, {_} -> 0}







share|improve this answer












You can just apply SparseArray to a SparseArray object to normalize it. For your example:



a = SparseArray[{2->22,3->33,5->55,7->77,11->1111,13->1313},13];
b = SparseArray[{1->11,3->33,5->55,7->77,9->99,11->1111},13];
r = DeleteCases[a-b,0];

SparseArray[r] //ArrayRules



{{1} -> -11, {2} -> 22, {9} -> -99, {13} -> 1313, {_} -> 0}








share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 30 '18 at 3:06









Carl Woll

67.1k388175




67.1k388175












  • For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
    – Leon
    Dec 30 '18 at 22:34


















  • For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
    – Leon
    Dec 30 '18 at 22:34
















For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
– Leon
Dec 30 '18 at 22:34




For simulating a row that changes and fills up somewhat, is it better to use SparseArray with + or Association with Merge?
– Leon
Dec 30 '18 at 22:34



Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg