zoukankan      html  css  js  c++  java
  • 初探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文件如下

    代码
    /* 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 = {11};

             
    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();
          }
       }
    }
  • 相关阅读:
    工资到帐的快乐排名第四
    如何成为一个具有批判性思维的人?
    别让千里马被驴踢死
    接口协议抓包与分析
    Qt + ffmpeg+SDl (转)
    各种流媒体服务器的架设(二)
    memcpy(转)
    java之public class和class声明区别详解 (转)
    cin、cin.get()、cin.getline()、getline()、gets()等函数的用法 (转)
    const_iterator 与 const iterator
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1688545.html
Copyright © 2011-2022 走看看