## Fast evaluation of modified dot product

Mathematica

### Next

• 1. help with slow minimize
Hi all, I am new to mathematica so bear with me. I have a relatively simple minimization problem: v1 = {v1x, v1y} v2 = {v2x, v2y} f[t_] := (1 - t)*v1 + t*v2 Minimize[{Sqrt[f[t].f[t]] + (1 - t)*d1 + t*d2, {t >= 0, t <= 1}}, t] // FullSimplify // Timing I have not yet been able to solve this on neither my Mac G5 nor my new MacBook (both with 2GB RAM). Mathematica runs out of memory after about two days. I assume I do something wrong? Can someone please give me a hint or explain why this problem is so difficult. I have already done the math by hand, and as I said it is straightforward, but I have more difficult minimizations I would like mathematica help me with. Best regards, Ola
• 2. Computing nCr how?
Hi, I know this sounds simple but I cant find the function in mathematica to compute nCr (i.e. number of combination for n obj chosen in groups of r). Can anyone help? Thanks
• 3. Can mathematica save restart file for Simplify?
Hello, I was wondering if mathematica can save restart file for Simplify. I am using v6.03 with Win XP pro. Though I have been running Simplify for about two months, I have not reached to an answer. Now I am afraid that some day I might encounter sudden power loss. I thought that if mathematica can save restart file for Simplify, this will surely be an powerful insurance. Can someone help me on this issue? -Toshi
• 4. vector-field plot
Hi, I have a unit vector of three components, Rx=Sin[p]*Cos[q]; Ry=Sin[p]*Sin[q]; Rz=Cos[p]; where, p=(Pi/2)*(1-z); and q=p*Cot[p]; I want to plot the vector-field of this unit vector in the region, {z, 0,1} and {q,0,0.5}

### Fast evaluation of modified dot product

```I have (below) a vector x and matrix y and would like to compute z
efficiently:

{n, m, b} = {10000, 100, 10};  (* n >> m > b *)
x = RandomReal[{-1.,1.}, n - b +1]
y = RandomChoice[{-1,1}, {n, m}];

w = Partition[y, b, 1];
z = Dot[x, w];

I have to compute z for many different x with w fixed. For large n, w
becomes prohibitively big.

Doing the below is much slower but doesn't require large memory
allocation:

z2 = Fold[(#1 + x[[#2]] y[[#2;;#2+b-1]]) &, 0., Range[Length[x]] ];

I was wondering if there is a good way to compute z that doesn't
require a lot of memory.

Thanks,
Art.

```

### Re: Fast evaluation of modified dot product

```

This is slow because y is not packed. If you use:

y = Developer`ToPackedArray@RandomChoice[{-1,1}, {n,m}];

and then run z2, it will be a bit faster than your Dot approach.

If speed is a major concern, and you don't mind a bit more memory usage,
then ListCorrelate might be a good option:

In:= MemoryInUse[]
MaxMemoryUsed[]

Out= 5850248
Out= 7176816

Your x, y modified to pack y:

In:= {n, m, b} = {10000, 100, 5};(*n>>m>b*)
x = RandomReal[{-1., 1.}, n - b + 1];
y = Developer`ToPackedArray@RandomChoice[{-1, 1}, {n, m}];

In:= MemoryInUse[]
MaxMemoryUsed[]

Out= 10861576

Out= 19951840

Using ListCorrelate:

In:= z3 = Transpose@ListCorrelate[{x}, Transpose@y]; // Timing

Out= {0.031,Null}

In:= MemoryInUse[]
MaxMemoryUsed[]

Out= 11210072

Out= 23306376

An increase of a bit more than 3 MB in max memory used. The results
aren't identical, because of the vagaries of machine arithmetic, but z3
runs about 15 times faster on my machine.

Carl Woll
Wolfram Research

```

### Re: Fast evaluation of modified dot product

```

Can be done with ListCorrelate.

Transpose[Map[ListCorrelate[x, #] &, Transpose[y]]]

Compared to your large list dot product approach, the memory hoofprint
will be negligeable. Also it is nearly 10x faster.

Daniel Lichtblau
Wolfram Research

```