初探java和matlab混合编程

最近要实现一个算法,需要用到矩阵运算,matlab的强项就是进行矩阵运算,所以要高效进行矩阵运算,就要在java中调用matlab。关于环境变量等的相关配置,请大家参考此文

http://www.cnblogs.com/allanyz/archive/2009/05/04/1449081.html

下面我要讲的例子是matlab自带的例子,位置在MATLABroot\R2008a\toolbox\javabuilder\Examples。

大家在matlab联机帮助文件中输入matlab build for java就能搜出此例子

个人认为这个例子比较精髓,通过这个例子可以完整的弄明白java是如何调用matlab进行矩阵运算的

此例的java文件如下

 

 

ExpandedBlockStart.gif 代码
/*  getfactor.java
 * This file is used as an example for the MATLAB
 * Builder JA product.
 *
 * Copyright 2001-2006 The MathWorks, Inc.
 
*/

/*  Necessary package imports  */
import  com.mathworks.toolbox.javabuilder. * ;
import  factormatrix. * ;

/*
 * getfactor class computes cholesky, LU, and QR 
 * factorizations of a finite difference matrix
 * of order N. The value of N is passed on the
 * command line. If a second command line arg
 * is passed with the value of "sparse", then
 * a sparse matrix is used.
 
*/
class  getfactor
{
   
public   static   void  main(String[] args)
   {
      MWNumericArray a 
=   null ;    /*  Stores matrix to factor  */
      Object[] result 
=   null ;     /*  Stores the result  */
      factor theFactor 
=   null ;    /*  Stores factor class instance  */

      
try
      {
         
/*  If no input, exit  */
         
if  (args.length  ==   0 )
         {
            System.out.println(
" Error: must input a positive integer " );
            
return ;
         }

         
/*  Convert input value  */
         
int  n  =  Integer.valueOf(args[ 0 ]).intValue();

         
if  (n  <=   0 )
         {
            System.out.println(
" Error: must input a positive integer " );
            
return ;
         }

         
/*
          * Allocate matrix. If second input is "sparse"
          * allocate a sparse array 
          
*/
         
int [] dims  =  {n, n};

         
if  (args.length  >   1   &&  args[ 1 ].equals( " sparse " ))
            a 
=  MWNumericArray.newSparse(dims[ 0 ], dims[ 1 ],n + 2 * (n - 1 ), MWClassID.DOUBLE, MWComplexity.REAL);
         
else
            a 
=  MWNumericArray.newInstance(dims,MWClassID.DOUBLE, MWComplexity.REAL);

         
/*  Set matrix values  */
         
int [] index  =  { 1 1 };

         
for  (index[ 0 =   1 ; index[ 0 <=  dims[ 0 ]; index[ 0 ] ++ )
         {
            
for  (index[ 1 =   1 ; index[ 1 <=  dims[ 1 ]; index[ 1 ] ++ )
            {
               
if  (index[ 1 ==  index[ 0 ])
                  a.set(index, 
2.0 );
               
else   if  (index[ 1 ==  index[ 0 ] + 1   ||  index[ 1 ==  index[ 0 ] - 1 )
                  a.set(index, 
- 1.0 );
            }
         }

         
/*  Create new factor object  */
         theFactor 
=   new  factor();

         
/*  Print original matrix  */
         System.out.println(
" Original matrix: " );
         System.out.println(a);

         
/*  Compute cholesky factorization and print results.  */
         result 
=  theFactor.cholesky( 1 , a);
         System.out.println(
" Cholesky factorization: " );
         System.out.println(result[
0 ]);
         MWArray.disposeArray(result);

         
/*  Compute LU factorization and print results.  */
         result 
=  theFactor.ludecomp( 2 , a);
         System.out.println(
" LU factorization: " );
         System.out.println(
" L matrix: " );
         System.out.println(result[
0 ]);
         System.out.println(
" U matrix: " );
         System.out.println(result[
1 ]);
         MWArray.disposeArray(result);

         
/*  Compute QR factorization and print results.  */
         result 
=  theFactor.qrdecomp( 2 , a);
         System.out.println(
" QR factorization: " );
         System.out.println(
" Q matrix: " );
         System.out.println(result[
0 ]);
         System.out.println(
" R matrix: " );
         System.out.println(result[
1 ]);
      }

      
catch  (Exception e)
      {
         System.out.println(
" Exception:  "   +  e.toString());
      }

      
finally
      {
         
/*  Free native resources  */
         MWArray.disposeArray(a);
         MWArray.disposeArray(result);
         
if  (theFactor  !=   null )
            theFactor.dispose();
      }
   }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值