3
$\begingroup$

I have huge matrices in the form of

mtx1 = {{24+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8]},{24+24 FF[5,10] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[7,8] GG[7,8]},{24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]+24 FF[7,8] GG[7,8],24+24 FF[5,10] GG[5,10]+24 FF[6,9] GG[5,10]+24 FF[7,8] GG[5,10]+24 FF[5,10] GG[6,9]+24 FF[6,9] GG[6,9]+24 FF[7,8] GG[6,9]+24 FF[5,10] GG[7,8]+24 FF[6,9] GG[7,8]}};

but the matrices I use are much bigger. Now I want to get rid of each term that contains FF[___] or GG[___]. Both always come together, therefore I used

mtx2 = mtx1 /. FF[___] -> 0; 
(* mtx2={{24, 24, 24}, {24, 24, 24}, {24, 24, 24}} *)

and got the desired result in mtx2. Unfortunatly it turns out that this zeroing is extremly time-consuming. For my huge matrices, it takes on the order of 100 seconds.

Question:

Is there a more time-efficient way to zero all FF[___]-terms in mtx1?

Comparison:

I compare several approaches, for a big 3 big test-matrix. The approaches also include the construction of the matrix.

my original approach

  • {174.8417751, 65.4913582, 25.3878123} seconds

Coefficient-Creation of Matrix

  • {134.4920621, 51.4260521sec, 19.6079772} seconds

belisarius' Block-evaluation methode

  • {82.3675688, 31.5639078, 12.3822025} seconds

eldo's Join/Partition

  • {77.8615328, 29.0973367, 11.2742769} seconds

kguler's Block-evaluation

  • {75.8906436, 29.1315892, 11.6544345} seconds

Mr.Wizard's mtx1[[All, All, 1]]

  • {75.5589726, 29.0378220, 11.9491954} seconds

Edit

The full problem, including the matrix-creation is posted here: Time-efficient creation of matrix

$\endgroup$
6
  • $\begingroup$ Why not a scope-aware redefinition FF[x__]:=0? $\endgroup$ Aug 12, 2014 at 16:18
  • $\begingroup$ Thought about that aswell. I am using FF[__] as undefined parameters, which with I continue calculating later. it should stay undefined. But maybe an intermediate definition and evaluation? $\endgroup$ Aug 12, 2014 at 16:20
  • 1
    $\begingroup$ Block[{FF}, FF[__] = 0; a = mtx1] $\endgroup$ Aug 12, 2014 at 16:40
  • $\begingroup$ Wow, even the matrix-construction is slower, your Block-evaluation outperforms my approach alot, see above. To have a fair comparison, i should also post the matrix construction method - for this i need some time. $\endgroup$ Aug 12, 2014 at 16:57
  • $\begingroup$ ... or a = Block[{F = 0 &}, mtx1] $\endgroup$
    – kglr
    Aug 13, 2014 at 10:56

2 Answers 2

2
$\begingroup$

For the example you gave FF[___] and GG[___] are the only non-number terms, therefore by polynomial sort order you could use simply:

mtx1[[All, All, 1]]
{{24, 24, 24}, {24, 24, 24}, {24, 24, 24}}

I shall now look at your newer question where I anticipate a more representative example.

$\endgroup$
3
  • $\begingroup$ LOL, you don't mind that I leave my answer for comparison purposes? $\endgroup$
    – eldo
    Aug 12, 2014 at 20:16
  • $\begingroup$ @eldo I didn't notice you were doing the same thing but verbosely. I found the question odd so I did no more than glance at your answer before posting. For some reason I thought you were doing something with Position but I misread. $\endgroup$
    – Mr.Wizard
    Aug 12, 2014 at 20:20
  • $\begingroup$ See above, your approach seems to be the fastest one. Nice solution, thanks! $\endgroup$ Aug 13, 2014 at 11:47
1
$\begingroup$

Given the structure of your SQUARE example matrix this should be fast:

dim = First @ Dimensions @ mtx1

3

tup = Join [#, {1}] & /@ Tuples[Range@dim, 2]

{{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {2, 1, 1}, {2, 2, 1}, {2, 3, 1}, {3,1, 1}, {3, 2, 1}, {3, 3, 1}}

Partition[mtx1[[#1, #2, #3]] & @@@ tup, dim]

{{24, 24, 24}, {24, 24, 24}, {24, 24, 24}}

Oneliner:

With[{dim = First@Dimensions@mtx1}, 
 Partition[mtx1[[#1, #2, #3]] & @@@ #, dim] &[
  Join [#, {1}] & /@ Tuples[Range@dim, 2]]]
$\endgroup$
1
  • $\begingroup$ This works very fast, thanks. I will write the compareing results down later. I posted the full problem (construction of a matrix, see above), for which this is a subproblem. Maybe you also have a solution for that? Thanks!! $\endgroup$ Aug 12, 2014 at 17:35

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.