Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Use of DPOSV function from Fortran
Date: Fri, 12 Sep 2008 15:18:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 171
Message-ID: <gae179$jpo$1@fred.mathworks.com>
References: <gabg0h$l3a$1@fred.mathworks.com> <gabok6$2d2$1@fred.mathworks.com> <gac07u$2va$1@fred.mathworks.com> <gad5ei$hkh$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1221232681 20280 172.30.248.35 (12 Sep 2008 15:18:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 12 Sep 2008 15:18:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1403623
Xref: news.mathworks.com comp.soft-sys.matlab:490012



"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gad5ei$hkh$1@fred.mathworks.com>...
> "Pam " <smithpamola@yahoo.com> wrote in message <gac07u$2va$1@fred.mathworks.com>...
> > Hello James,
> > 
> > Thank you so much for the help. Actually you got me right...I had the fortran program, I converted it but it gives an error that 'undefined function - dposv'.
> > 
> > I am new to Matlab so can u elaborate more on that. I tried to run 'mex lapack.c' in Matlab (in windows) as he mentioned but it gives me an error again - 
> > '??? Error using ==> mex
> > Unable to complete successfully'.
> > 
> > I understood that we need to pass no of arguments as many they are in original function.
> > dot = lapack('Z=ZDOTC(i,z,i,z,i)', length(x), x, 1, y, 1);
> > Now, i,z are just undefiend varibales, right? I mean they first i=length(x), z =x,.... right?
> > 
> > I have original dposv function like this:
> > dposv('A',6,1,a,6,b,12,c);
> > Now, I want it to work with Matlab. :(
> > 
> > Thanks again...
> > -Pam
> > 
> 
> OK. First I will tell you how to call the LAPACK routine as you requested ... and then I will tell you the easy way.
> 
> 1) As you requested ...
> 
> First download the lapack files from the FEX (looks like you already did this). Make sure the files are somewhere on the MATLAB path, and change your default directory to be that directory. Then to mex the file, do this:
> 
> mex -setup
> (then press Enter)
> (then select a C compiler, such as lcc)
> (then press Enter a couple more times)
> mex lapack.c
> 
> You should get a lapack.mexw32 file. This is the actual dll file that will be executed. If you are not on a Windows system, then the instructions in the lapack.c file say to do this (I haven't tried this, since I have a Windows system):
> 
> mex -ldl lapack.c
> 
> Once you have the mex routine built, here is an example sequence of commands to call the LAPACK DPOSV function from the MATLAB workspace using the same variable sizes and names that you were using:
> 
> >> a = rand(6,6)
> 
> a =
> 
>     0.5466    0.5226    0.8757    0.2987    0.5828    0.5798
>     0.4449    0.8801    0.7373    0.6614    0.4235    0.7604
>     0.6946    0.1730    0.1365    0.2844    0.5155    0.5298
>     0.6213    0.9797    0.0118    0.4692    0.3340    0.6405
>     0.7948    0.2714    0.8939    0.0648    0.4329    0.2091
>     0.9568    0.2523    0.1991    0.9883    0.2259    0.3798
> 
> >> a = a'*a  % ensure pos def
> 
> a =
> 
>     2.9124    1.8632    1.8098    1.9438    1.6328    1.9507
>     1.8632    2.1749    1.4346    1.5142    1.2682    1.8440
>     1.8098    1.4346    2.1680    1.0484    1.3289    1.4108
>     1.9438    1.5142    1.0484    1.8088    1.0089    1.5163
>     1.6328    1.2682    1.3289    1.0089    1.1347    1.3233
>     1.9507    1.8440    1.4108    1.5163    1.3233    1.7933
> 
> >> b = rand(12,1)
> 
> b =
> 
>     0.7833
>     0.6808
>     0.4611
>     0.5678
>     0.7942
>     0.0592
>     0.6029
>     0.0503
>     0.4154
>     0.3050
>     0.8744
>     0.0150
> 
> >> c = 0
> 
> c =
> 
>      0
> 
> >> [out1 out2 out3] = lapack('DPOSV(h,i,i,D,i,D,i,I)','U',6,1,a,6,b,12,c)
> 
> out1 =
> 
>     1.7066    1.0918    1.0605    1.1390    0.9568    1.1431
>     1.8632    0.9914    0.2792    0.2729    0.2255    0.6012
>     1.8098    1.4346    0.9826   -0.2399    0.2558    0.0313
>     1.9438    1.5142    1.0484    0.6160   -0.1316    0.0938
>     1.6328    1.2682    1.3289    1.0089    0.2929    0.3360
>     1.9507    1.8440    1.4108    1.5163    1.3233    0.0509
> 
> 
> out2 =
> 
>  -116.1058
>   102.8057
>   -62.7881
>   145.5830
>   422.0414
>  -364.5099
>     0.6029
>     0.0503
>     0.4154
>     0.3050
>     0.8744
>     0.0150
> 
> 
> out3 =
> 
>            0
> 
> Note that the vector b is 12x1 but we only used the first 6x1 part of it according to your function call, so I did the same. Thus, the answer in out2 only has a result for the first 6x1 part as expected.
> 
> If you can't get lapack.c compiled and working properly, then a custom C mex file could be built that calls the DPOSV subroutine to get your answer.
> 
> An explanation of this:
> 
> [out1 out2 out3] = lapack('DPOSV(h,i,i,D,i,D,i,I)','U',6,1,a,6,b,12,c)
> 
> DPOSV is the subroutine to be called. The argument prototype works like this (small case means input only, large caps case means output and possibly also an input). I constructed this prototype myself by reading the LAPACK documentation for the DPOSV subroutine:
> 
> h = The first argument is a character input
> i = The second argument is an integer input
> i = The third argument is an integer input
> D = The fourth argument is a double output/input
> i = The fifth argument is an integer input
> D = The sixth argument is a double output/input
> i = The seventh argument is an integer input
> I = The eighth argument is an integer output/input
> 
> Then after the character string describing the prototype, you list the arguments. I could not find the option 'A' that you were using in the DPOSV documentation, so I used 'U' meaning that the input data is in the upper triangular portion of A (actually I could have also used 'L' in this case since the data is in both places). Then I just listed the other arguments. Since there are three CAP letters in the prototype, that means I am expecting three outputs, so I use [out1 out2 out3] to capture them. You do not have to convert MATLAB double variables to int32 variables in those spots where DPOSV is expecting an integer ... the lapack function will do this automatically for you inside the mex routine (nice!).
> 
> 2) The easy way ...
> 
> I checked the doc for DPOSV and it states that this routine simply computes the solution to a real system of linear equations A * X = B, where A is an n-by-n symmetric positive definite matrix and X and B are n-by-nrhs matrices. Well, since all you are doing is solving a system of linear equations you can forget all about that lapack stuff I just showed you and simply use the MATLAB built in backslash operator to solve the system of equations. MATLAB is very good at solving systems of linear equations:
> 
> >> a\b(1:6)
> 
> ans =
> 
>  -116.1058
>   102.8057
>   -62.7881
>   145.5830
>   422.0414
>  -364.5099
> 
> Same answer as before. Nicer!
> 
> James Tursa
> 


---------------
Hey James,

Thanks a lot!
Its a great help. I really appreciate it.

For option 2, it does not work for a=12*12 and b=1*12 matrix. I tried a\b(12:1), but that doesn't work either. Let me know if you have any idea about it.

Thanks a million....

-Pam