Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*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 2000

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

Search the Archive

Re: Could this be improved?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21791] Re: Could this be improved?
  • From: "Luci Ellis" <elisha at dot.net.au>
  • Date: Thu, 27 Jan 2000 22:57:28 -0500 (EST)
  • References: <86mdpv$2f3@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Jordan,

I have a two, largely similar, suggestions.  They both cut the time taken
roughly in half, although both your version and mine are roughly quadratic
in time.

Test data:
arr[100] = Array[f, 100];
arr[300] = Array[f, 300];
arr[500] = Array[f, 500];
arr[700] = Array[f, 700];
arr[900] = Array[f, 900];
arr[1100] = Array[f, 1100];

Your version:
myMtx[v_] := Module[
    {nCols, nRows, vPadded, c},
    nCols = Length[v];
    nRows = 2nCols - 1;
    c = ZeroMatrix[nRows, nCols];
    vPadded = PadRight[v, nRows, 0];
    For[i = 1, i <= nCols, i++,
      c[[All, i]] = vPadded;
      vPadded = RotateRight[vPadded]
      ];
    c
    ]


Timing[myMtx[arr[100]];]
{0.15 Second, Null}

Timing[myMtx[arr[300]];]
{1.18333 Second, Null}

Timing[myMtx[arr[500]];]
{3.46667 Second, Null}

Timing[myMtx[arr[700]];]
{6.86667 Second, Null}

Timing[myMtx[arr[900]];]
{11.65 Second, Null}


My versions:
Note the absence of a loop, and that there aren't local variables being
defined and redefined, which is very time-consuming.  Also I tend to use
With[] rather than Module[].  With[] is for defining local _constants_,
rather than local variables, and is apparently a bit faster than Module in
that context.  (See David Wagner's "Power Programming in Mathematica"
book, which I recommend if you do want to become more familiar with the
Mathematica way of programming).

myMtx2[v_] := With[{nCols = Length[v]},
    With[{vPadded = PadRight[v, 2 nCols - 1, 0]},
      Transpose[Table[RotateRight[vPadded, i], {i, 0, nCols - 1}]] ]]


myMtx3[v_] := With[{nCols = Length[v]},
    With[{vPadded = PadRight[v, 2 nCols - 1, 0]},
      Transpose[RotateRight[vPadded, #] & /@ Range[0, nCols - 1]] ]]

Timing results:

Timing[myMtx2[arr[100]];]
{0.05 Second, Null}

Timing[myMtx2[arr[300]];]
{0.566667 Second, Null}

Timing[myMtx2[arr[500]];]
{1.68333 Second, Null}

Timing[myMtx2[arr[700]];]
{3.3 Second, Null}

Timing[myMtx2[arr[900]];]
{5.71667 Second, Null}

Timing[myMtx3[arr[100]];]
{0.0666667 Second, Null}

Timing[myMtx3[arr[300]];]
{0.566667 Second, Null}

Timing[myMtx3[arr[500]];]
{1.68333 Second, Null}

Timing[myMtx3[arr[700]];]
{3.3 Second, Null}

Timing[myMtx3[arr[900]];]
{5.7 Second, Null}

Others may have better ideas, but nonetheless I hope this helps.

Regards,
Luci


----------
In article <86mdpv$2f3 at smc.vnet.net>, "Jordan Rosenthal" <jr at ece.gatech.edu>
wrote:


> Hi all,
>
> I wrote the following code which works correctly.  I was wondering, however,
> if there was a way of doing the same thing that had more of a Mathematica
> approach.  I am new to Mathematica and am still trying to get a grasp on how
> to program effectively within the environment.
>
> myMtx[v_] := Module[
>     {nCols, nRows, vPadded, c},
>     nCols = Length[v];
>     nRows = 2nCols - 1;
>     c = ZeroMatrix[nRows, nCols];
>     vPadded = PadRight[v, nRows, 0];
>     For[i = 1, i <= nCols, i++,
>       c[[All, i]] = vPadded;
>       vPadded = RotateRight[vPadded]
>       ];
>     c
>     ]
>
> For example, myMtx[{1,2,3}] takes the vector {1,2,3} and turns it into the
> matrix {{1, 0, 0}, {2, 1, 0}, {3, 2, 1}, {0, 3, 2}, {0, 0, 3}} which looks
> like
>
> [ 1 0 0 ]
> [ 2 1 0 ]
> [ 3 2 1 ]
> [ 0 3 2 ]
> [ 0 0 3 ]
>
>
> Thanks,
>
> Jordan
>
>
>
>
> 


  • Prev by Date: Re: Problem with evaluation of delayed rules
  • Next by Date: RE: DSolve problems with system of ODEs. Solutions and Thanks
  • Previous by thread: RE: Could this be improved?
  • Next by thread: Making a function dynamically define another conditional function...