MathGroup Archive 1999

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

Search the Archive

Re: CompiledFunction for matrices ??

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16089] Re: CompiledFunction for matrices ??
  • From: dreiss at !SPAMscientificarts.com (David Reiss)
  • Date: Thu, 25 Feb 1999 08:24:52 -0500
  • Organization: EarthLink Network, Inc.
  • References: <7atr4s$7m9@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <7atr4s$7m9 at smc.vnet.net>, "Ersek, Ted R"
<ErsekTR at navair.navy.mil> wrote:

> While writing some code to answer a question Peter Klamser sent in I found
> myself wanting to write a CompiledFunction that takes a length (n) vector of
> real numbers.
> The best solution I could find is to explicitly write out (n) variables as
> in:
> foo=Compile[{x1,x2,x3, ... , xn}, expr]
> 
> Then I can evaluate something like:
> Apply[foo, {6.2,4.1,2.5,8.6,7.7,9.1,2.2,1.4}]
> 
> If I want to write a CompiledFunction that takes an (m) by (n) matrix of
> real numbers it's also a real chore by any method I know of.
> 
> The documentation says:
> Compile[{{x1, t1, n1}, ... }, expr] assumes that xi is a 
> rank ni array of objects each of a type which matches ti.
> 
> I used to think this allowed the sort of thing I am looking for, but  I
> didn't think about it long enough.  It seems
> Compile[{{m,_Real,n}}, expr]   (for n>2) takes a tensor.
> 
> __________________
> 
> So how can we write a CompiledFunction that takes a large vector or large
> matrix?
> 
> 
> Thanks,
> Ted Ersek


Hi Ted,


If I understand your question correctly you are confusing the "n"
in Compile[{{m,_Real,n}}, expr] with the size of (number of elements in)
the object in question.  I had this confusion when I first encountered
this new feature in Version 3.  Actually n=1 corresponds to a vector
of arbitrary length, n=2 corresponds to a 2-dimensional 
rectangular matrix, etc...

For example, this computes the dot product of a (Real) 
vector with itself:


In[1]:=
testCompile1=Compile[{{x, _Real,1 }}, x.x]

Out[1]=
CompiledFunction[{x},x.x,"-CompiledCode-"]


Here is a length 1000 vector:

In[2]:=
testVector=Table[Random[],{j,1000}];

In[3]:=
testCompile1[testVector]

Out[3]=
332.942


Check the result:


In[4]:=
testVector.testVector

Out[4]=
332.942


This computes the product of an p x q matrix with its transpose:


In[5]:=
testCompile2=Compile[{{x, _Real,2 }}, x.Transpose[x]]

Out[5]=
CompiledFunction[{x},x.Transpose[x],"-CompiledCode-"]


Here is a 2 x 1000 matrix:


In[6]:=
testMatrix=Table[Random[],{i,2},{j,1000}];

In[7]:=
testCompile2[testMatrix]

Out[7]=
{{326.89,246.928},{246.928,334.346}}


Check the result:


In[8]:=
testMatrix.Transpose[testMatrix]

Out[8]=
{{326.89,246.928},{246.928,334.346}}


Does this answer the question?


Regards,

David

-- 


   
----------------------------------------
 
Scientific Arts:
Creative Services and Consultation 
for the Applied and Pure Sciences 

David Reiss 
Email: dreiss at !SPAMscientificarts.com 

----------------------------------------
 


Remove the !SPAM to send email


  • Prev by Date: Re: Strange results from Integrate
  • Next by Date: Re: How can I plot for array variable?
  • Previous by thread: Re: CompiledFunction for matrices ??
  • Next by thread: Bug using Options[]