1. 程式人生 > >apache commons Math的簡介和使用

apache commons Math的簡介和使用

     apache commons Math是一組偏向科學計算為主的函式,主要是針對線性代數,數學分析,概率和統計等方面。

     我雖然是數學專業畢業,當年也是抱著《數學分析》啃,但是好久不用,這些概念都開始生疏,寫一點例子,僅作參考。

packagetest.ffm83.commons.math;

importorg.apache.commons.math3.linear.Array2DRowRealMatrix; 

import org.apache.commons.math3.linear.LUDecomposition; 

importorg.apache.commons.math3.linear.RealMatrix; 

importorg.apache.commons.math3.stat.descriptive.moment.GeometricMean; 

importorg.apache.commons.math3.stat.descriptive.moment.Kurtosis; 

importorg.apache.commons.math3.stat.descriptive.moment.Mean; 

importorg.apache.commons.math3.stat.descriptive.moment.Skewness; 

importorg.apache.commons.math3.stat.descriptive.moment.StandardDeviation; 

importorg.apache.commons.math3.stat.descriptive.moment.Variance; 

import org.apache.commons.math3.stat.descriptive.rank.Max; 

importorg.apache.commons.math3.stat.descriptive.rank.Min; 

importorg.apache.commons.math3.stat.descriptive.rank.Percentile; 

importorg.apache.commons.math3.stat.descriptive.summary.Product; 

importorg.apache.commons.math3.stat.descriptive.summary.Sum; 

importorg.apache.commons.math3.stat.descriptive.summary.SumOfSquares;

/**

 * 簡單使用commons Math方法

 * @author 範芳銘

 */

public class MathUsage {

     public static void main(String[] args) { 

        double[] values = new double[] { 0.33, 1.33,0.27333, 0.3, 0.501, 

                0.444, 0.44, 0.34496, 0.33,0.3, 0.292, 0.667 }; 

        Min min = new Min(); 

        Max max = new Max(); 

        Mean mean = new Mean(); // 算術平均值 

        Product product = new Product();//乘積 

        Sum sum = new Sum(); 

        Variance variance = new Variance();//方差 

        System.out.println("min: " +min.evaluate(values)); 

        System.out.println("max: " +max.evaluate(values)); 

        System.out.println("mean: " +mean.evaluate(values)); 

        System.out.println("product:" + product.evaluate(values)); 

        System.out.println("sum: " +sum.evaluate(values)); 

        System.out.println("variance:" + variance.evaluate(values)); 

        Percentile percentile = newPercentile(); // 百分位數 

        GeometricMean geoMean = newGeometricMean(); // 幾何平均數,n個正數的連乘積的n次算術根叫做這n個數的幾何平均數 

        Skewness skewness = new Skewness(); //Skewness(); 

        Kurtosis kurtosis = new Kurtosis(); //Kurtosis,峰度 

        SumOfSquares sumOfSquares = newSumOfSquares(); // 平方和 

        StandardDeviation StandardDeviation =new StandardDeviation();//標準差 

        System.out.println("80 percentilevalue: " 

                + percentile.evaluate(values,80.0)); 

        System.out.println("geometricmean: " + geoMean.evaluate(values)); 

        System.out.println("skewness:" + skewness.evaluate(values)); 

        System.out.println("kurtosis:" + kurtosis.evaluate(values)); 

        System.out.println("sumOfSquares:" + sumOfSquares.evaluate(values)); 

       System.out.println("StandardDeviation: " +StandardDeviation.evaluate(values)); 

       System.out.println("-------------------------------------"); 

        // Create a real matrix with two rowsand three columns 

        double[][] matrixData = { {1d,2d,3d},{2d,5d,3d}}; 

        RealMatrix m = newArray2DRowRealMatrix(matrixData); 

        System.out.println(m); 

        // One more with three rows, twocolumns 

        double[][] matrixData2 = { {1d,2d},{2d,5d}, {1d, 7d}}; 

        RealMatrix n = newArray2DRowRealMatrix(matrixData2);         

        // Note: The constructor copies  the input double[][] array.          

        // Now multiply m by n 

        RealMatrix p = m.multiply(n); 

       System.out.println("p:"+p); 

       System.out.println(p.getRowDimension());    // 2 

       System.out.println(p.getColumnDimension()); // 2          

        // Invert p, using LUdecomposition 

        RealMatrix pInverse = newLUDecomposition(p).getSolver().getInverse(); 

        System.out.println(pInverse); 

    } 

}

執行結果如下:

min: 0.27333

max: 1.33

mean: 0.46269083333333333

product: 2.3429343978460972E-5

sum: 5.552289999999999

variance: 0.08757300031742428

80 percentile value: 0.5674000000000001

geometric mean: 0.4112886050879374

skewness: 2.670095445623868

kurtosis: 7.718241303328169

sumOfSquares: 3.5322966905000004

StandardDeviation: 0.2959273564870681

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

Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0}}

p:Array2DRowRealMatrix{{8.0,33.0},{15.0,50.0}}

2

2

Array2DRowRealMatrix{{-0.5263157895,0.3473684211},{0.1578947368,-0.0842105263}}

相關推薦

Apache Commons pool 簡介pool連線池程式碼

在實際中工作,我們經常遇到需要連線池的地方,特別是資料庫連線池。 我們為什麼需要池呢?因為這些資源的建立,都很消耗資源。因此,我們使用一個物件池,裡面預先建立了一些資源物件。當我們需要時,從池中取出物件,而不需要時,把物件返回池中。這樣就可以提高程式碼執行的效率。 Apac

apache commons Math簡介使用

     apache commons Math是一組偏向科學計算為主的函式,主要是針對線性代數,數學分析,概率和統計等方面。      我雖然是數學專業畢業,當年也是抱著《數學分析》啃,但是好久不用,這些概念都開始生疏,寫一點例子,僅作參考。 packagetest.f

使用apache.commons.compress壓縮加壓檔案工具類

package com.my.common.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.archivers.ArchiveEntry; import o

Apache commons(Java常用工具包)簡介

機制 encode 解析 help IT PE tom base cit Apache Commons是一個非常有用的工具包,解決各種實際的通用問題,下面是一個簡述表,詳細信息訪問http://jakarta.apache.org/commons/index.html Be

Apache Commons工具集簡介

轉自:http://zhoualine.iteye.com/blog/1770014         Apache Commons包含了很多開源的工具,用於解決平時程式設計經常會遇到的問題,減少重複勞動。下面是我這幾年做開發過程中自己用過的工具類做簡單介紹。 元件 功能介紹 BeanUtils 提供了對於

java apache commons HttpClient傳送getpost請求的學習整理

HttpClient 是我最近想研究的東西,以前想過的一些應用沒能有很好的實現,發現這個開源專案之後就有點眉目了,令人頭痛的cookie問題還是有辦法解決滴。在網上整理了一些東西,寫得很好,寄放在這裡。 HTTP 協議可能是現在 Internet 上使用得最多、最重要

[Apache commons系列]DBUtils簡介-2.核心類簡介

  DbUtils是一個小型的類庫,不需要也不值得花太長的時間去熟悉每一個類。DbUtils核心其實只有三個類/介面,即QueryRunner 、ResultSetHandler 和DbUtls (官方文件中寫的是前兩個)。(來源:http://blog.csdn.n

Apache Arrow原始碼分析(一)——簡介框架

背景 列儲存在資料庫領域中早已被提出,列儲存資料結構在分析型事務上表現優異,大資料分析引擎,諸如Spark-SQL,Impala 均採用列儲存作為其中間資料表示形式,那麼Apache Arrow就是這樣一種記憶體列式資料結構。 在眾多分散式系統中,每

org.apache.http.client.HttpClientorg.apache.commons.httpclient.HttpClient的區別

最近看專案的程式碼,看到工程中有兩個jar包張的很像,一個是commons.httpclient-3.1.jar,一個是httpclient4.2.1.jar,很納悶,而且這兩個包裡都有HttpClient這個類,但是包名卻不一樣,然後就查找了一番資料,看下這兩個包到底是

Apache Commons 簡介

擴展 tex fun base 外部 字符串 狀態機 pre servlet Apache Commons 由多個獨立發布的軟件包組成,此頁面提供了當前可用的 Commons 組件的概述。 Components BCEL 字節碼工程庫 - 分析,創建和操作

day1 python簡介入門

back argv 安裝gcc www 導入 urn 16px 利用 表示 Linux的yum依賴自帶Python,為防止錯誤,此處更新其實就是再安裝一個Python: 安裝Python   1、下載安裝包 https://www.python.

Apache的主要目錄配置文件詳解

監聽 erl 腳本 env tca live 斷開連接 linux real 一、Apache 主要配置文件註釋Apache的主配置文件:/etc/httpd/conf/httpd.conf默認站點主目錄:/var/www/html/Apache服務器的配置信息全部存儲在主

KBEngine warring項目源碼閱讀(一) 項目簡介註冊登錄

urn 創建 ges input alt 接下來 F12 .com name 首先介紹下warring項目,是kbe自帶的一個演示示例,大部分人了解kbe引擎也是從warring項目開始的。 項目地址:https://github.com/kbengine/kbengine

Apache Commons Digester 二(規則模塊綁定-RulesModule、異步解析-asyncParse、xml變量Substitutor、帶參構造方法)

對象 property 解決 space getclass bool trace throw object 前言 上一篇對Digester做了基本介紹,也已經了解了Digester的基本使用方法,接下來將繼續學習其相關特性,本篇主要涉及以下幾個內容: 規則模塊綁定,

C#取整函數Math.Round、Math.CeilingMath.Floor

取整 c# mat logs color pre log clas 偶數 1.Math.Round:四舍六入五取偶 引用內容 Math.Round(0.0) //0 Math.Round(0.1) //0 Math.Round(0.2) //0 Math.Round(0

【FTP】org.apache.commons.net.ftp.FTPClient實現復雜的上傳下載,操作目錄,處理編碼

ttr hide working log 登錄 有一個 ima spl att 和上一份簡單 上傳下載一樣 來,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/

Python自學之路【第一篇】:Python簡介入門

youtube 通用 too 互聯網公司 python腳本 bar strong 重裝 排行 Python前世今生 python的創始人為吉多·範羅蘇姆(Guido van Rossum)。1989年的聖誕節期間,吉多·範羅蘇姆為了在阿姆斯特丹打發時間,決心開發一個新的腳本

java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

apache con ont test oca action error esp iat 1.java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils 缺少類 2. There is no Ac

Rust 1.7.0 匹配器 match 的簡介使用

let 滿足 選擇 多個 efault msg i++ pretty article 使用過正則表達式的人應該都知道 matcher ,通過 matcher 匹配器運算正則表達式,完畢一系列的匹配規則。 在Rust 中 沒有 switch 語句。mat

java.lang.ClassNotFoundException: org.apache.commons.dbutils.QueryRunner

ica ror server acc dwr comm erro tor reads 七月 28, 2017 11:06:33 下午 org.apache.catalina.core.StandardWrapperValve invoke嚴重: Servlet.serv