Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MatrixTemplate (was SpecialMatrix)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28457] Re: [mg28414] MatrixTemplate (was SpecialMatrix)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 19 Apr 2001 03:26:50 -0400 (EDT)
  • References: <933FF20B19D8D411A3AB0006295069B028696B@dassne02.darmstadt.dsh.de>
  • Sender: owner-wri-mathgroup at wolfram.com

Carl, Hartmut,

It looks as if Packed Arrays are causing some of the features noted by
Helmut.
Incidentally the output from CreateMatrix seems to be the normal matrix (not
packed).

I have (again!) changed notation.

Template1[x_, dims_]:=
  Fold[PadRight[{},#2,{#1}]&, x, Reverse[dims]]

Template2[x_, dims_]:=
  Fold[Table[Evaluate[#1],{#2}]&, x, Reverse[dims]]

p = 250;q= 100;r=100;

Template1 gives an ordinary tensor.

r1=Template1[1.1,{p,q,r}];//Timing
Developer`PackedArrayQ[r1]

        {0. Second,Null}

        False

Template2 give a packed array and is much slower.

r2=Template2[1.1,{p,q,r}];//Timing
Developer`PackedArrayQ[r2]

        {3.74 Second,Null}

        True

Make a packed form of r1 and an unpacked form of r2

r1pkd= Developer`ToPackedArray[r1];//Timing
r2unpkd= Developer`FromPackedArray[r2];//Timing

        {2.25 Second,Null}

        {12.47 Second,Null}

The longer time for making r2 seems to be because of making  the packed
form. But, if we want a packed array it looks as if packing r1 to get r1pkd
is the quickest way.

It seems that comparing packed with unpacked forms takes more time; though
not as much as if both sides were being made of the same type: comparing
packed forms seems to be the quickest.

r1===r2//Timing
r1pkd ===r2//Timing
r1 === r2unpkd//Timing
r1pkd===r2unpkd//Timing

        {5.38 Second,True}

        {1.6 Second,True}

        {8.29 Second,True}

        {4.23 Second,True}

If we reduce p to 249 then we Template2 no longer gives a packed array, and
the timings are comparable

p = 249;q= 100;r=100;

r1=Template1[1.1,{p,q,r}];//Timing
Developer`PackedArrayQ[r1]

        {0. Second,Null}

        False

r2=Template2[1.1,{p,q,r}];//Timing
Developer`PackedArrayQ[r2]

        {0. Second,Null}

        False

Packing will not occur with symbolic entries: for these Template1 only shows
up quicker for large tensors.

p = 500;q= 500;r=500;

Template1[xx,{p,q,r}];//Timing
Developer`PackedArrayQ[r1]

        {0.11 Second,Null}

        False

Template2[xx,{p,q,r}];//Timing
Developer`PackedArrayQ[r2]

        {0.71 Second,Null}

        False


Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

----- Original Message -----
From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
To: mathgroup at smc.vnet.net
<carlw at u.washington.edu>
Subject: [mg28457] RE: [mg28414] MatrixTemplate (was SpecialMatrix)


> Dear Allan, dear Carl,
>
> these are most miraculous discoveries you both made.
> The Question arises, what makes CreateMatrix such fast?
> and what did you really get? a fully laid-out matrix?
> This is not so, I believe. For my investigation
> please see below...
>
> > -----Original Message-----
> > From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> > Sent: Tuesday, April 17, 2001 5:54 AM
> > To: mathgroup at smc.vnet.net
> > Subject: [mg28457] [mg28414] MatrixTemplate (was SpecialMatrix)
> >
> >
> > The problem of creating a matrix with all the entries the
> > same, say all
> > zero, has cropped up in recent postings.
> > The following seems to be very quick way of doing this:
> >
> > Clear["`*"]
> > CreateMatrix[x_,m_,n_]:=
> >   PadRight[{#},m,{#}]&[PadRight[{x},{n},{x}]]
> >
> > Check
> >
> > CreateMatrix[0,3,5]
> >
> >         {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
> >
> > Timings
> >
> > CreateMatrix[0,5000,5000];//Timing
> > CreateMatrix[0,5000,2500];//Timing
> > CreateMatrix[0,2500,5000];//Timing
> > CreateMatrix[0,5000,250];//Timing
> > CreateMatrix[0,250,5000];//Timing
> >
> >         {0. Second,Null}
> >
> >         {0.11 Second,Null}
> >
> >         {0. Second,Null}
> >
> >         {0.06 Second,Null}
> >
> >         {0.16 Second,Null}
> >
> > For comparison:
> >
> > IdentityMatrix[5000];//Timing
> >
> >         {10.27 Second,Null}
> >
> > and. using Table,
> >
> > Clear["`*"]
> > CreateMatrix[x_,m_,n_]:=
> >   Table[Evaluate[Table[x,{n}]],{m}]
> >
> > CreateMatrix[0,3,5]
> >
> >         {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
> >
> > CreateMatrix[0,5000,5000];//Timing
> > CreateMatrix[0,5000,2500];//Timing
> > CreateMatrix[0,2500,5000];//Timing
> > CreateMatrix[0,5000,250];//Timing
> > CreateMatrix[0,250,5000];//Timing
> >
> >         {10.22 Second,Null}
> >
> >         {5. Second,Null}
> >
> >         {4.67 Second,Null}
> >
> >         {0.44 Second,Null}
> >
> >         {0.55 Second,Null}
> >
> > --
> > Allan
> > ---------------------
> > Allan Hayes
> > Mathematica Training and Consulting
> > Leicester UK
> > www.haystack.demon.co.uk
> > hay at haystack.demon.co.uk
> > Voice: +44 (0)116 271 4198
> > Fax: +44 (0)870 164 0565
> >
> >
> >
> >
> [Hartmut Wolf]
>
> In[1]:= $Version
> Out[1]= "4.0 for Microsoft Windows (April 21, 1999)"
> In[2]:= mem0 = MemoryInUse[]
> Out[2]= 1129872
> In[3]:= k0 = 80554 K ;
>
> ...this is the memory used as read off Microsoft Task Manager
> at my computer (Windows 2000).
>
> Now we go through a sequence of steps, which are obvious:
>
> In[4]:=
> m1 = PadRight[{#}, 2000, {#}] &@PadRight[{}, 3000, 0]; // Timing
> Out[4]= {0.01 Second, Null}
> In[5]:= mem1 = MemoryInUse[]
> Out[5]= 1158200
> In[6]:= k1 = 80532 K;
>
> In[7]:=
> m2 = PadRight[{{}}, {2000, 3000}, 0]; // Timing
> Out[7]= {1.752 Second, Null}
> In[8]:= mem2 = MemoryInUse[]
> Out[8]= 25217776
> In[9]:= k2 = 104100 K;
>
> In[10]:=
> m3 = Table[0, {2000}, {3000}]; // Timing
> Out[10]= {2.965 Second, Null}
> In[11]:= mem3 = MemoryInUse[]
> Out[11]= 49221952
> In[12]:= k3 = 127560 K;
>
>
> ...just for comparison, a Matrix of same size:
>
> In[13]:=
> m4 = Partition[Range[2000*3000], 3000]; // Timing
> Out[13]= {0.17 Second, Null}
> In[14]:= mem4 = MemoryInUse[]
> Out[14]= 73225392
> In[15]:= k4 = 151024 K;
>
> Now let's look at the memory used (per element):
>
> In[16]:=
> Subtract @@@ Partition[{mem4, mem3, mem2, mem1, mem0}, 2, 1]/(2000.*3000.)
> Out[16]=
> {4.00057, 4.0007, 4.00993, 0.00472133}
>
> We see m2, m3, m4 use up four bytes/element each, which is what we
> expect. However m1 is much more compact!
>
> In[17]:=
> Subtract @@@ Partition[{k4, k3, k2, k1, k0}, 2, 1]
> Out[17]=
> {23464 K, 23460 K, 23568 K, -22 K}
>
> This is memory consumption as read off the MS TaskManager.
> This is roughly consistent with recorded MemoryInUse.
>
> In[18]:= MemoryInUse[]
> Out[18]= 73231408
> In[19]:= kk0 = 151412 K;
>
> Now we work with our "objects"
>
> In[20]:= m3 == m4 // Timing
> Out[20]= {0. Second, False}
>
> It takes no time, to find out the discrepancy, ok.
>
> In[21]:= MemoryInUse[]
> Out[21]= 73234112
> In[22]:= kk1 = 151424 K;
>
> ...and the comparison takes no memory, ok.
> However...
>
> In[23]:= m1 == m4 // Timing
> Out[23]= {4.797 Second, False}
>
> ... the comparison with m1 takes up more time, than was
> gained in first place! Not ok!
>
> In[24]:= MemoryInUse[]
> Out[24]= 73236368
> In[25]:= kk2 = 275144 K;
>
> ...what happend here? Mathematica doesn't show additional
> memory used, however much had been reclaimed from the OS!
>
> In[26]:= m2 == m4 // Timing
> Out[26]= {8.372 Second, False}
> In[27]:= MemoryInUse[]
> Out[27]= 73238624
> In[28]:= kk3 = 275144 K;
>
> ..also much time for m2 needed!
>
> In[29]:= m1 == m2 // Timing
> Out[29]= {0.521 Second, True}
> In[30]:= MemoryInUse[]
> Out[30]= 73240896
>
> ...this is fast
>
> In[31]:= kk4 = 275144 K;
> In[32]:= m2 == m3 // Timing
> Out[32]= {5.217 Second, True}
> In[33]:= MemoryInUse[]
> Out[33]= 73243144
>
> In[34]:= kk5 = 275544 K;
> In[35]:= m1 == m3 // Timing
> Out[35]= {4.887 Second, True}
> In[36]:= MemoryInUse[]
> Out[36]= 73245496
> In[37]:= kk6 = 275168 K;
>
> I admit that these last timings are not fully conclusive,
> and might be somewhat dependend on the structure of the hardware
> used (This is a notebook computer from my company:
> hp OmniBook 4150, and possibly there is a performance
> step down at 128 MB due to chip set, furthermore there
> could be some influence of paging for the last operations
> (paging times are not recorded by Timing[] though),
> my real memory was 192 MB).
>
> I will refrain from fantasizing about what is going on,
> whowever someone familiar with "Mathematica inside"
> is asked to comment. Esp. to the question: what must be
> paid for "rapid matrix creation", and when?
>
> Kind regards, Hartmut
>




  • Prev by Date: Re: circular plot question
  • Next by Date: Re: Summation Formulae Table
  • Previous by thread: RE: MatrixTemplate (was SpecialMatrix)
  • Next by thread: Can Mathematica be used as expert system (rule-based computations like CLIPS?)