MathGroup Archive 2008

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

Search the Archive

Re: Compiling SingularValueDecomposition

  • To: mathgroup at
  • Subject: [mg89830] Re: Compiling SingularValueDecomposition
  • From: Szabolcs Horvát <szhorvat at>
  • Date: Sun, 22 Jun 2008 03:22:12 -0400 (EDT)
  • References: <g3ihod$fc9$>

Frank Hu wrote:
> Hi, group,
> I'm trying to speed up a piece of code that uses  
> SingularValueDecomposition by compiling it.  But I couldn't get it to  
> compile.  For a simple demonstration, try
> fx=Compile[{{x, _Real, 2}},  SingularValueDecomposition[x],  
> {{SingularValueDecomposition, _Real, 3}}]
> and fx[[4]] is
> {{1, 5}, {54, Function[{x}, SingularValueDecomposition[x]], 3, 2, 0,  
> 3, 2, 1}, {2}}
> The "Function" there tells fx wasn't compiled successfully.  Calling  
> fx[{{1., 2.}, {3., 4.}}] will generate the following warnings
> CompiledFunction::cfte: Compiled expression {<<1>>} should be a rank  
> 2 tensor of machine-size real numbers. >>
> CompiledFunction::cfex: Could not complete external evaluation at  
> instruction 2; proceeding with uncompiled evaluation. >>
> Any suggestions?

There is no point in trying to compile SingularValueDecomposition.  It 
is already a highly optimized function, implemented in a low level 
language for machine precision numbers, so it should be nearly as fast 
as a C version for large enough matrices.

But we still have to explain why Mathematica throws that error.  There 
is a small mistake in the pattern that you used.  Try this (but remember 
that it does not speed things up):

fx = Compile[{{x, _Real, 2}},
   {{SingularValueDecomposition[_], _Real, 3}}]

The above example will work for the 2*2 test input, but keep in mind 
that generally SingularValueDecomposition does *not* return a rank 3 
tensor (it returns a list of 3 matrices), so it will not work for other 

Here's a list of compilable functions:

Unfortunately it was written for version 3.0, but it is still useful as 
a guideline for version 6.0/

  • Prev by Date: Re: Question - Cluster Analysis
  • Next by Date: Re: Insight into Solve...
  • Previous by thread: Re: Compiling SingularValueDecomposition
  • Next by thread: Re: Compiling SingularValueDecomposition