2013-05-18

お手軽 Firefox OS Simulator

Firefox OS は、Mozillaが開発している、モバイル端末向けのオープンソースな OS(オペレーティングシステム)です。プロジェクト名は、Boot to Gecko, B2G です。

モバイル端末向けの OS といえば、Apple iOS や Google Android が普及しており、後発の OS として Tizen や Firefox OS が話題になっています。これらの後発の OS は、Android のカーネルを出発点としてはいるものの、HTML5 + JavaScript のアプリケーションが動作するようになっており、Apple や Google のような特定の企業の独自性のあるサービスに縛られずに、標準化された規格 (HTML5) の元でアプリケーションを開発できます。

Firefox の Add-on で動作する Firefox OS Simulator

日本ではありませんが、Firefox OS を OS に搭載した端末が 6 月にもデビューするということで、関連情報を調べていたのですが、その時に Web Browser の Firefox の Add-on 用の Firefox OS Simulator を見つけました。

このアドオン・アプリケーションは、Preview にも関わらず、Version 3.0 になっているので、遅ればせながら、ということかもしれませんが、試してみましたので簡単に紹介します。Firefox を起動して、上記サイトから Firefox OS Simulator の Add-on をインストールすればすぐに使えますが、Firefox を起動するのがあまりにも久しぶりで、Add-on のアプリケーションを通常の状態から起動する方法が判らず、結構探すことになりましたので、参考までに起動するメニューの場所を以下に示しました。

Add-on の Firefox OS Simulator を起動すると下記の画面が表示されます。"Stopped" と表示されているボタンをクリックすると、"Runnning" に表示が代わり、Firefox OS のシミュレータウィンドウが開きます。Console のチェックを入れておくと、エラーを表示するコンソールウィンドウも開きます。

シミュレータのハードコピーをいくつか紹介します。

Linux は Dual Core の 64bit とは言えど、もう古い PC なので、こういうシミュレータ(エミュレータ)を起動すると結構な負荷がかかって、冷却ファンの回転数が最大になりうんうん唸りだします。こういう時に新しい PC がとても欲しくなります。

ちなみに OS のバージョンは Boot2Gecko 1.1.0.0-prerelease となっています。

HTML5 に本格的に取り組むのはもう少し先でもいいかなと思って、個人としては JavaFX にプログラミングの重点を置いていますが、実務レベルでは JavaScript の方がはるかに需要が高く、HTML5 を前面に出したこういうモバイル端末向け OS を見てしまうと、たいへんな焦燥感にかられます。う~ん、悩ましいです。時代の流れがこっちの方だと直感的には感じているからです。

この週末にもう少し使ってみて、詳しい紹介ができれば、と考えています。

参考サイト

  1. Firefox OSは2013年6月に5カ国でデビューさせるとMozillaのCEOが表明
  2. Firefox OS搭載の開発者向け端末、発売数時間で売り切れ
  3. まだチャンスはあるのか?Firefox OSレビュー
  4. Firefox OSの展開拡大、KDDIも取り組み

2013-05-04

【備忘録】JavaFX で複数のウィンドウを扱う

5 月の連休は、4/26 - 5/5 とまとまって取ることができました。この休みこそは普段の仕事を忘れ、ゆっくり JavaFX で自分が作りたいアプリケーション作りに没頭したいものだと思っていたのですが、なかなかそうはいきませんでした。

連休までに終わらせる予定だった仕事が終わらず、休みに入っても最初の三日間は自宅にこもって徹夜で対応していました。そのためすっかり昼夜が逆転してしまって調子を崩し、やっと自分のやりたいことが出きるようになった頃には、もう連休は後半にさしかかっています。

とにかく、GUI アプリケーションを作るための土台を作ろうと意気込んだのは良いのですが、どうも勝手がわかりません。メニューバーの ヘルプ からこのプログラムについて みたいなアイテムを選んで、別のウィンドウを表示する、と言った、ほんの基本的なことを実現するだけでも、なかなか納得したものができません。いろいろサイトを調べたりしてたどり着いた結果を備忘録としてもとめておきました。出来てしまえばなんてことはなかったのですが…。

やりたかったことは以下の通りです。

先述したように、メニューバーのメニュー Help から、項目 About を選んで、別ウィンドウを表示させるというものです。

メニューバーの Help メニューまわりは以下の様にします(メインプログラムの Ammonite.java の一部)。

public class Ammonite extends Application {
    ...
    (省略)
    ...
    @Override
    public void start(final Stage stage) {
       ...
       (省略)
       ...
        BorderPane root = new BorderPane();
        root.setPrefSize(300, 100);

        MenuBar bar = new MenuBar();
        root.setTop(bar);
       ...
       (省略)
       ...
        Menu helpMenu = new Menu("_Help");
        MenuItem aboutItem = new MenuItem("_About");
        aboutItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                Stage aboutStage = new DlgAbout(stage);
                aboutStage.show();
            }
        });
       ...
       (省略)
       ...
    }

    public static void main(String[] args) {
        launch(args);
    }
}

DlgAbout クラス (DlgAbout.java) は以下の様になります。プログラム固有の変数などが含まれていますので、あくまで参考まで。

public class DlgAbout extends Stage {

    Image logo = new Image(getClass().getResourceAsStream(Ammonite.imgLogo));
    Image info = new Image(getClass().getResourceAsStream(Ammonite.imgInfo));

    public DlgAbout(Stage owner) {
        initOwner(owner);
        initModality(Modality.APPLICATION_MODAL);
        initStyle(StageStyle.UTILITY);
        setTitle("About this");
        setResizable(false);
        getIcons().add(info);

        VBox vbox = new VBox();
        vbox.setId("dlg");

        Label lab1 = new Label(Ammonite.appName);
        lab1.setStyle("-fx-font-size: 20pt;");
        vbox.getChildren().add(lab1);

        Label lab2 = new Label("version " + Ammonite.appVer);
        vbox.getChildren().add(lab2);

        Label lab3 = new Label("COPYRIGHT(C) "
                + Ammonite.appYear + ", " + Ammonite.appAuthor);
        vbox.getChildren().add(lab3);

        Label lab4 = new Label("running on Java "
                + System.getProperty("java.version"));
        vbox.getChildren().add(lab4);

        Label lab5 = new Label();
        lab5.setGraphic(new ImageView(logo));
        lab5.setId("logo");
        vbox.getChildren().add(lab5);

        Button but1 = new Button("Close");
        vbox.getChildren().add(but1);
        but1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                hide();
            }
        });

        Scene scene = new Scene(vbox);
        scene.getStylesheets().add(Ammonite.class.getResource("DlgAbout.css").toExternalForm());

        setScene(scene);
    }
}

一つできれば、他に別ウィンドウを作る場合にも応用が容易です。ある程度の汎用目的に使える基本的なダイアログを作ることもできます。

ちなみに DlgAbout クラスでは setResizable(false); として、ウィンドウサイズの変更をユーザー側ではできないように設定していますが、Fedora で使っているデスクトップ環境 LXDE では、この設定が効きません。Windows XP 上では下記の様に確かに設定が有効になっています。ウィンドウの右上隅の違いです(ウィンドウを最大/最小にするボタン(?)がありません)。一方で、info アイコンが左上隅に表示されません。

さて、この Ammonite というアプリケーションですが、エンジニアリングで必要な統計解析ができるツールになる予定ですが、まだ中身がありません。TableView を使いこなせなくてモタモタしています。むかしむかし、Tcl/Tk で作りかけていたアプリケーションを、JavaFX で作り直そうとしています。ベースの機能ができてくれば、SourceForge.net あたりにレポジトリを公開します。ご興味がある方は、何卒、気長に待っていてください。

参考サイト

  1. Set dialog Modality.APPLICATION_MODAL : Dialog « JavaFX « Java

2013-04-29

今度の Fedora 19 は…

7 月にリリースされる予定の Fedora 19 についてですが、開発コードネームはちょっと意味深な Schrödinger's Cat(シュレディンガーの猫)になっています。例年だと、ゴールデンウィークの頃には、次のリリースのβ版か、RC 版が出ていて試すことができる時期ですが、現 Fedora 18 のリリースが今年の 1 月 15 日と、2 ヶ月ほど遅れた影響で、Fedora 19 のリリースもズレてしまっています。α版が 4 月 23 日にリリースされましたが試す勇気はありません。リリーススケジュールは遅れるのが常ですので、今度はきっと、お盆休みぐらいが Fedora 19 へ移行するのに良い時期になるのでは、と予想。

さて、Fedora 19 に収録されるパッケージの目玉は、Releases/19/FeatureList によると以下の通りです。個人的に興味があるものについては、行を薄い赤の背景色にしました。

Name Summary
3D Printing Bring 3D printing tools to Fedora and allow users of 3D printers, such as RepRap, to be fully satisfied with software in the repositories, without having to download binary blobs or run Python code from git.
AnacondaNewUI Followup The purpose of this feature is to describe the high level work items we have for anaconda related to newui in F-19.
Anaconda Realm Integration Kickstart will have a 'realm join example.com' command, to join the machine during install to an AD or FreeIPA domain. This will take place using one time passwords or password-less joins to an AD or FreeIPA domain.
BIND10 BIND10 is next generation of the popular BIND9 DNS server rewritten from scratch. BIND10 suite includes both DNS and DHCP service servers.
CUPS 1.6 Update CUPS to the latest upstream release and use PDF rather than PostScript as baseline document format.
Checkpoint/Restore Add support to checkpoint and restore processes. Checkpointing processes can be used for fault tolerance and/or load balancing.
Developers Assistant Perform a series of various changes to improve developer experience on Fedora.
Dracut HostOnly Only create "host-only" initramfs images. A generic fallback image will be generated, if it does not exist and never ever be removed.
Erlang/OTP R16 Update Erlang to the upstream R16 release.
Fedora 19 Boost 1.53 Uplift This feature brings Boost 1.53.0 to Fedora 19.
Federated VoIP Make it easier for the deployment of federated SIP and XMPP (Jabber) networks, functioning much like federated SMTP email.
firewalld Lockdown This feature adds a simple configuration setting for firewalld to be able to lock down configuration changes from local applications.
firewalld Rich Language This feature adds a rich (high level) language to firewalld, that allows to easily create complex firewall rules without the knowledge of iptables syntax.
First-Class Cloud Images This feature expands Fedora's current cloud image deliverables beyond just EC2, and overhauls how they are produced. The goal is to produce cloud images for EC2 and other cloud deployments for the Alpha, Beta, and Final compose process and distribute them on the mirror network. There will also be nightly or weekly image builds for Rawhide to assist with early development. All images should be constructed using a newer generation of tools.
FreeIPA Two Factor Authentication Provide Kerberos enabled, LDAP replicated, two-factor authentication for FreeIPA.
GCC 4.8.x Switch GCC in Fedora 19 to 4.8.x, rebuild all packages with it.
GLIBC 2.17 Switch GLIBC in Fedora 19 to GLIBC version 2.17
GNOME 3.8 Update GNOME to the latest upstream release
Guile2 Update GNU Guile to version 2.0.7 in Fedora 19.
High Availability Container Resources The Container Resource feature allows the HA stack (Pacemaker + Corosync) residing on a host machine to extend management of resources into virtual guest instances (KVM/Linux Containers) through use of the pacemaker_remote service.
FreeIPA v3 Trust Improvements Multiple Domain Controllers and multiple additional DNS domains managed by FreeIPA can now be accessible via trusting relationship by Active Directory domain members. Additionally, Global Catalog service is provided for use by AD clients, allowing FreeIPA users to be included into access-control lists of AD resources.
Features/JRuby 1.7 JRuby is an alternative Ruby implementation with fast growing user base due to its great performance in parallel tasks. Although JRuby 1.6.7 is already in Fedora, this feature brings in new minor version and better Fedora integration.
Java 8 Add a tech preview preview of the the upcoming version of Java (OpenJDK8) to Fedora 19
KDE Plasma Workspaces 4.10 Rebase to KDE Plasma Workspace 4.10 including Plasma Desktop and Netbook workspaces, the KDE Applications and the KDE Platform.
KScreen Replace current KDE screen management software by KScreen.
Less Brittle Kerberos Make kerberos in Fedora simpler to use by removing some of the brittleness that are common failure points. In particular we remove the need for kerberos clients to sync their clocks, and remove the need to have reverse DNS records carefully setup for services.
MATE Desktop 1.6 MATE Desktop is based on GNOME 2 and provides an intuitive and attractive desktop to Linux users who seek a simple, easy to use traditional interface.
MEMSTOMP Include the MEMSTOMP DSOs in Fedora 19 to enable developers to more quickly detect certain library calls which result in undefined behaviour due to overlapping memory arguments.
MinGW GCC 4.8 Update the mingw-gcc cross-compiler to gcc 4.8 and rebuild all MinGW packages against it
More Mobile Broadband New mobile broadband devices supporting multiple technologies (eg, CDMA/EVDO/LTE and/or GSM/UMTS/LTE) and using new proprietary protocols are becoming common in the marketplace, and are not well supported by ModemManager 0.6 and earlier. We developed ModemManager 0.7/0.8 with a new API specifically to address this issue and to be more compatible with future mobile broadband devices.
NFStest Provides a set of tools for testing either the NFS client or the NFS server, most of the functionality is focused mainly on testing the client.
New firstboot This feature proposes new initial setup application with better integration to the NewUI anaconda and to Gnome Initial Experience.
Node.js The Node.js JavaScript runtime and associated ecosystem, including the npm package manager.
Ease Of Use: System Management with OpenLMI Add providers and capabilites to the OpenLMI infrastructure that would ease the remote system management.
OpenShift Origin OpenShift Origin is a cloud application platform as a service (PaaS). It is the open sourced, community supported version of OpenShift
OpenStack Grizzly OpenStack will be upgraded to the next major stable release, called "Grizzly". In addition the new OpenStack "heat" and "ceilometer" incubation projects will be included.
Feature Name: Performance Co-Pilot Feature Update A new feature release of PCP (Performance Co-Pilot).
PHP 5.5 To provide the latest PHP stack.
Pillow Replace PIL (python-imaging) with Pillow, an actively maintained fork which is also heading for python3 compatibility
QXL/Spice KMS Driver Currently the QXL driver is X.org only, a KMS driver is required to move forward with projects like spice 3D, and also to allow more features to be show in virt environments like plymouth.
Update RPM to 4.11 Update RPM to 4.11 in Fedora 19.
Realmd FreeIPA Support realmd currently supports discovery and configuring of Active Directory domains. With this feature it will also include support for FreeIPA domains.
Remove PyXML from Fedora The goal of this Feature is to remove the PyXML package from Fedora.
Replace MySQL with MariaDB MariaDB, a community developed fork of MySQL, will be the default implementation of MySQL in Fedora 19.
Ruby 2.0.0 Ruby 2.0.0 is the latest stable version of Ruby, with major increases in speed and reliability. With this major update from Ruby 1.9.3 in Fedora 18 to Ruby 2.0 in Fedora 19, alongside JRuby, Fedora becomes the superior Ruby development platform.
Ryu Network Operating System Ryu Network Operating System http://osrg.github.com/ryu/
SSSD improve AD integration The next major release of SSSD will include support for more advanced AD features for domain members. This includes site support and trusted domains. Additionally it will include a plugin for the cifs-utils package which would allow a CIFS client to use SSSD for lookups which were currently only possible with winbind.
Scratch for Fedora Scratch is an educational programming environment which makes it easy to create games, animations, and art. It's open source and would be a great addition to Fedora.
Shared System Certificates Make NSS, GnuTLS, OpenSSL and Java share a default source for retrieving system certificate anchors and black list information. This is an initial but useful step in the direction of a comprehensive solution.
Simplify Java/Maven Packaging using XMvn Introduce new Maven packaging tooling with new macros, automated install section and more
Syslinux Option This feature will make Syslinux an optional bootloader for Fedora, in kickstart and via a hidden Anaconda option. When used this way, it will replace grub2.
systemd Calendar Timers systemd has supported timer units for activating services based on time since its inception. However, it only could schedule services based on monotonic time events (i.e. "every 5 minutes"). With this feature in place systemd also supports calendar time events (i.e. "every monday morning 6:00 am", or "at midnight on every 1st, 2nd, 3rd of each month if that's saturday or sunday").
systemd Lightweight Containers For a longer time systemd already included the systemd-nspawn tool as a more powerful version of chroot(1), primarily inteded for use in development, experimenting, debugging, instrumentation, testing and building of software. With Fedora 19 we want to make nspawn considerably more useful, so that it can easily be used to start containers capable of booting up a complete and unmodified Fedora distribution inside as normal system services.
systemd Message Catalog Logging is essential for finding and tracking down system problems. Just finding and tracking them down however is seldom enough to actually get them fixed. With Journal Message Catalogs we want to link helpful meta information directly to many log messages applications generate, keyed off an ID identifying the type of message. This localized meta information can help the user to fix the problem, refer him to additional documentation, or even inform him where to get further help.
systemd/udev Predictable Network Interface Names The udevd service has a long history of providing predicatable names for block devices and others. For Fedora 19 we'd like to provide the same for network interfaces, following a similar naming scheme, but only as fallback if not other solution such as biosdevname is installed or the administrator manually defined network interface names via udev rules or the old network scripts.
systemd Resource Control systemd already has support for assigning specific resources to system services using various configuration settings. With Fedora 19 we'd like to build on that, and add the ability for the admin to dynamically query the resource control parameters and change them at runtime.
Systemtap 2.2 A new feature release of Systemtap
Thermostat 1.0 Improved monitoring and serviceability tool for Java applications
Trusted Network Connect (TNC) This feature provides Trusted Network Connect(TNC) framework that can be used to assess and verify end clients' system state (such as network ports/firewall status or legitimate binaries) and its compliance to a predefined policy with existing network access control (NAC) solutions.
Virt Storage Migration Migrate a running virtual machine from one host to another, including in use storage, with no downtime. No need for a shared storage location between the two.
Virtio RNG Provide a paravirtual random number generator to virtual machines, to prevent entropy starvation in guests.
Add LVM Thin provisioning support to the yum-fs-snapshot plugin For the purposes of system rollback: Provide the ability to create a snapshot of all thinly provisioned LVM2 volumes associated with FS mount points that are relevant to a yum transaction.
Yum Groups as Objects Change the default yum configuration from group_command=compat to group_command=objects.
GSS Proxy The main purpose of this project is to replace rpc.svcgssd(8), the server-side rpcsec_gss daemon.
libkkc libkkc, a new Japanese Kana Kanji input library, will be available in Fedora 19, along with an IBus input method engine which uses libkkc as backend (ibus-kkc). ibus-kkc will be the default Japanese input method in place of ibus-anthy.

関連サイト

  1. 2013年4月24日 “シュレディンガーの猫”がやっとお目覚め─Fedora 19アルファ版がリリース

2013-04-17

Oracle、Javaやデータベースのアップデートを一挙公開 新たなセキュリティ対策も実装

Java 7 update 21 が 4 月 16 日にリリースされました。今回は気がついたのが早く、比較的タイムリーにブログの記事にすることができました。早速、Linux (64bit) 用の RPM をダウンロードして更新しました。

2013-04-02

【備忘録】jOpenDocument

Java で利用できるスプレッドシートのクラスが公開されていないかと探しているのですが、なかなか手頃なものが見つかりません。スプレッドシートと言っても、Excel のような高機能なものではなく、シンプルで好きなように加工しやすいものが欲しいのですが、いつもオープンソースで公開されていることを期待するのは、ちと虫のいい願いなのかもしれません。ここは諦めてスクラッチから自作するしかなさそうです。

目的とするものには出会えていませんが、jOpenDocument という面白そうなライブラリを見つけたので備忘録として紹介します。

jOpenDocemt は、OASIS で標準化された OpenDocument フォーマットを操作するための Java で記述されたライブラリです。ライセンスは GPL か(二種類の)商用ライセンスで配布されています。今回試したバージョンは jOpenDocument-1.3rc2.jar (Version 1.3 rc 2, March 11, 2013) です。Java 6 以上の環境が必要です。

使用環境

  • OS : Fedora 18 (Linux)
  • JDK : jdk-1.7.0_17-fcs.x86_64
  • IDE : NetBeans IDE 7.3
  • (libreoffice-calc-3.6.5.2-8)

以下は、紹介されているサンプルそのままを自分の環境にコピーしたものです。

package jopendocumenttest;

import java.io.File;
import java.io.IOException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jopendocument.dom.OOUtils;
import org.jopendocument.dom.spreadsheet.SpreadSheet;

public class JOpenDocumentTest {

    public static void main(String[] args) throws IOException {
        // Create the data to save.
        final Object[][] data = new Object[6][2];
        data[0] = new Object[]{"January", 1};
        data[1] = new Object[]{"February", 3};
        data[2] = new Object[]{"March", 8};
        data[3] = new Object[]{"April", 10};
        data[4] = new Object[]{"May", 15};
        data[5] = new Object[]{"June", 18};

        String[] columns = new String[]{"Month", "Temp"};

        TableModel model = new DefaultTableModel(data, columns);

        // Save the data to an ODS file and open it.
        final File file = new File("temperature.ods");
        SpreadSheet.createEmpty(model).saveAs(file);

        OOUtils.open(file);
    }
}

実行例を以下に示しました。

プログラムを実行すると、自分の環境にインストールされている LibreOffice Calc が起動されます。これは作成した文書の内容を確認するためです。プログラムの主目的は文書を作成する事です、一応念のため。

何かオフィス系の文書などの作成業務を自動化できる用途に威力を発揮できそうです。

参考サイト

  1. jOpenDocument Homepage. Open Document library

2013-04-01

NetBeans IDE で Commons Math を試す

Cmmons Math a. は 、Apache のトッププロジェクトである Apache Commons にある Apache Commons#Commons Proper に属する、数学と統計学の軽量コンポーネントです b.Apache License Version 2.0 に従って配布されています。いろいろなコンポーネントを利用できますが、まずは、今までの流れで多項式係数を最小二乗法で求めてみます。

JLAPACK の時と同じサンプル、100000 組の xy が対になっているデータ fdata01.zip (fdata01.txt) を使って、JLAPACK の時と同じように c.、次の 4 次の多項式でフィッティングするために、その係数 c0,  c1,  c2,  c3,  c4 を最小二乗法で求めます。

y = c0 + c1x + c2x2 + c3x3 + c4x4

使用環境

  • OS : Fedora 18 (Linux)
  • JDK : jdk-1.7.0_17-fcs.x86_64
  • IDE : NetBeans IDE 7.3

以下が Commons Math を利用したサンプルプログラムです。なお、使用した Commons Math は Version 3.1.1 (09 January 2013) です。なお、Java 1.5 以上が必要です。

package commonsmathtest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;

public class CommonsMathTest {

    public static void main(String[] args) {
        int m = 100000, n = 5;
        double X[][], y[];
        X = new double[m][n];
        y = new double[m];

        File file = new File("/home/bitwalk/work/ex101_01/fdata01.txt");

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String str;
            int i = 0;
            while ((str = br.readLine()) != null) {
                double xValue = Double.parseDouble(str.substring(0, 6));
                y[i] = Double.parseDouble(str.substring(6, 20));
                for (int j = 0; j < n; j++) {
                    X[i][j] = Math.pow(xValue, (double) j);
                }
                i++;
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }

        OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
        regression.setNoIntercept(true);
        regression.newSampleData(y, X);

        double[] c = regression.estimateRegressionParameters();
//        double[] residuals = regression.estimateResiduals();
//        double[][] parametersVariance = regression.estimateRegressionParametersVariance();
//        double regressandVariance = regression.estimateRegressandVariance();
        double rSquared = regression.calculateRSquared();
//        double sigma = regression.estimateRegressionStandardError();

        // Result
        System.out.println("# of DATA\t" + m);
        System.out.println("Coefficients");
        for (int j = 0; j < n; j++) {
            System.out.println("C(" + j + ") =  " + c[j]);
        }
        System.out.println("R-squared = " + rSquared);
    }
}

この線形重回帰式を求めるクラスでは、デフォルトでは切片項 c0 = c0 × x0x0 をモデル行列 X に含める必要はありません。含める場合は、setNoIntercepttrue にする必要があります d.。このサンプルでは他のサンプルとやり方を揃えるために true としています。

実行例を以下に示しました。ここでは R2 (決定係数)も計算しています。

run:
# of DATA 100000
Coefficients
C(0) =  37.76585776133079
C(1) =  0.04968384703750148
C(2) =  6.988983959849906E-6
C(3) =  -4.217424916004741E-7
C(4) =  2.686562674530066E-10
R-squared = 0.7654053656031088
ビルド成功(合計時間: 1秒)

同じ次数で比較した場合、JAMA より時間がかかっているように見えますが、n を大きくすると(すなわち、多項式の次数を大きくすると)計算速度に違いが見られ、JAMA よりは速いが、JLAPACK には劣ると言ったところです。しかし、線形問題ばかりでなく、他の機能もいろいろと揃っているので使い甲斐がありそうです。

参考サイト

  1. Commons Math: The Apache Commons Mathematics Library
  2. Apache Commons Math
  3. NetBeans IDE で JLAPACK を試す (2)
  4. The Commons Math User Guide - Statistics 1.5 Multiple linear regression

2013-03-29

NetBeans IDE で JAMA を試す

JAMA(JAva MAtrix package) a. は Java で基本的な線形代数計算を行うためのパブリックドメインのライブラリです。

JLAPACK の時と同じサンプル、100000 組の xy が対になっているデータ fdata01.zip (fdata01.txt) を使って、JLAPACK の時と同じように b.、次の 4 次の多項式でフィッティングするために、その係数 c0,  c1,  c2,  c3,  c4 を最小二乗法で求めてみました。

y = c0 + c1x + c2x2 + c3x3 + c4x4

使用環境

  • OS : Fedora 18 (Linux)
  • JDK : jdk-1.7.0_17-fcs.x86_64
  • IDE : NetBeans IDE 7.3

以下が JAMA を利用した場合のサンプルプログラムと実行例です。なお、使用した JAMA は Version 1.0.3 (November 9, 2012) です。

package jamatest;

import Jama.Matrix;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class JamaTest {

    public static void main(String[] args) {
        int m = 100000, n = 5;
        double xMat[][], yVec[];
        xMat = new double[m][n];
        yVec = new double[m];

        File file = new File("/home/bitwalk/work/ex101_01/fdata01.txt");

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String str;
            int i = 0;
            while ((str = br.readLine()) != null) {
                double xValue = Double.parseDouble(str.substring(0, 6));
                yVec[i] = Double.parseDouble(str.substring(6, 20));
                for (int j = 0; j < n; j++) {
                    xMat[i][j] = Math.pow(xValue, (double) j);
                }
                i++;
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }

        // xMat -> X
        Matrix X = new Matrix(xMat, m, n);
        // yVec -> y
        Matrix y = new Matrix(yVec, m);
        // solution for y = Xc
        Matrix c = X.solve(y);
        // Result
        System.out.println("# of DATA\t" + m);
        System.out.println("Coefficients");
        for (int j = 0; j < n; j++) {
            System.out.println("C(" + j + ") =  " + c.get(j, 0));
        }
    }
}
run:
# of DATA 100000
Coefficients
C(0) =  37.76585776133068
C(1) =  0.04968384703746971
C(2) =  6.988983959987343E-6
C(3) =  -4.2174249160063376E-7
C(4) =  2.686562674530643E-10
ビルド成功(合計時間: 0秒)

この程度の次数の行列計算では、JLAPACK との差を感じませんが、このサンプルの n の値を大きくすると差が顕著に判るようになります。つまり JAMA の方が遅いのですが、Java での扱いやすさは、JLAPACK に勝っていると思います。

参考サイト

  1. JAMA: Java Matrix Package
  2. NetBeans IDE で JLAPACK を試す (2)

2013-03-24

NetBeans IDE で JLAPACK を試す (2)

JLAPACK ライブラリが NetBeans IDE 上で利用できるようになったので、以前、gfortran + LAPACK で試したサンプルを、Java + JLAPACK でも試してみました。参考サイト a. で、LAPACK の最小二乗問題を扱う倍精度用の手続き副プログラム DGELS b. を利用した QR 分解のサンプルを紹介していますので、これを Java 用に書き換えました。

サンプルは、100000 組の x と y が対になっているデータ fdata01.zip (fdata01.txt) を読み込んで、下記の4次の多項式に最小二乗法でフィッティングしたときの係数 c1c5 を求めるというプログラムです。

y = c0 + c1x + c2x2 + c3x3 + c4x4

上記 gfortran のサンプルプログラム ex101_01.f を Java に書き直した DgelsTest.java を以下に示します。

package dgelstest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.netlib.lapack.Dgels;
import org.netlib.util.intW;

public class DgelsTest {
    public static void main(String[] args) {
        intW info = new intW(2);
        int m = 100000, n = 5;
        int nrhs = 1, lda = m, ldb = m, lwork = 2 * n;

        double a[], b[], work[];
        a = new double[m * n];
        b = new double[m];
        work = new double[lwork];

        File file = new File("/home/bitwalk/work/ex101_01/fdata01.txt");

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String str;
            int i = 0;
            while ((str = br.readLine()) != null) {
                double x = Double.parseDouble(str.substring(0, 6));
                b[i] = Double.parseDouble(str.substring(6, 20));
                for (int j = 0; j < n; j++) {
                    a[i + m * j] = Math.pow(x, (double) j);
                }
                i++;
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }

        Dgels.dgels("N", m, n, nrhs, a, 0, lda, b, 0, ldb, work, 0, lwork, info);

        if (info.val == 0) {
            System.out.println("# of DATA\t" + m);
            System.out.println("Coefficients");
            for (int j = 0; j < n; j++) {
                System.out.println("C(" + j + ") =  " + b[j]);
            }
        } else {
            System.out.println("Error!");
            System.out.println("info = " + info.val);
        }
    }
}

実行結果は以下のようになりました。

run:
# of DATA 100000
Coefficients
C(0) =  37.76585776132993
C(1) =  0.04968384703750248
C(2) =  6.988983959858456E-6
C(3) =  -4.2174249160048426E-7
C(4) =  2.686562674530099E-10
ビルド成功(合計時間: 0秒)

いまどきの PC(と言っても、自分の PC は結構古いのですが)だと、Java を使っても、 100000 x 5 の行列計算程度では、データの読み込み時間を含めて大した時間がかかりません。

Dgels クラスでは、行列の要素を一次元配列に配置するので、C や Java の二次元配列における行と列の関係が、Fortran では逆になることを意識しなければなりません。なお、JLAPACK では、行列を二次元配列で扱える、DGELS c. というクラスも用意されているはずなのですが、このクラスがなぜか認識されなかったので、試すことができませんでした。

参考サイト

  1. LAPACK の利用例 - Workshop Complex at bitWalk
  2. Dgels - The Innovative Computing Laboratory (ICL) のサイトに掲載されている JLAPACK の文書
  3. DGELS - 同上
  4. NetBeans IDE で JLAPACK を試す - NetBeans IDE 上でライブラリの使い方を説明

2013-03-20

NetBeans IDE で JLAPACK を試す

JLAPACK は、は f2j により変換された Java 版の LAPACK です。

LAPACK (Linear Algebra PACKage) とは、Fortran で記述された数値解析ソフトウェアライブラリ(BSD ライセンス)のことで、線型方程式や線型最小二乗問題、固有値問題、特異値問題等を数値的に解くために利用されています。これを f2j という、Fortran ソースから Java ソースおよびクラスファイルへ翻訳するツールを用いて生成されたものが JLAPACK です。

Java でアプリケーションを作るにあたって(十分に枯れていて)計算結果に信頼ができて、使いやすい数値計算ライブラリを探しています。まずは、Fortran や C で自分でも使用実績がある LAPACK の Java 版を評価してみることにしましたので、NetBeans IDE 上での使い方を備忘録としてまとめました。

使用環境

  • OS : Fedora 18 (Linux)
  • JDK : jdk-1.7.0_17-fcs.x86_64
  • IDE : NetBeans IDE 7.3

まず、JLAPACK をダウンロード a.して、適当な場所に展開します((jlapack-0.8.tgz)。

NetBeans IDE を起動して、メニューから ツール(T)Ant ライブラリ(L) を選ぶと、「Ant ライブラリ・マネージャ」のダイアログが表示されますので、新規ライブラリ(N)... ボタンをクリックして新しいライブラリ名を指定します。ここでは、追加するライブラリを JLAPACK としました。そして、JLAPACK の jar ファイルをライブラリのクラスパスに追加します。

動作確認用にプロジェクトを作成します。ここでは、jlapack-0.8.tgz を展開したディレクトリ jlapack-0.8 内の examples/DgesvdTest.java を使用しました。このプロジェクトで JLAPACK ライブラリを利用するため、プロジェクトの『ライブラリ』を右クリックして『ライブラリの追加...』を選択します。

『ライブラリの追加』ダイアログから、JLAPACK を選び、ライブラリの追加 ボタンをクリックしてプロジェクトのライブラリに追加します。

下記は実行した結果です。

機会があれば、具体的なサンプルを試した結果を紹介します。

参考サイト

  1. java/f2j - Netlib にける JLAPACK のダウンロードサイト
  2. LAPACK — Linear Algebra PACKage - Netlib の LAPACK のサイト
  3. LAPACK Routines - kusuhara's wiki - Fortran での説明ですが、構造解析で使いそうなルーチンの一覧がまとめられています。
  4. NetBeans IDE で JLAPACK を試す (2) - 最小二乗問題を扱うサンプルを紹介しています。

2013-03-08

「Java 7 Update 17」公開、既に悪用が確認されている脆弱性を修正

3 月 4 日に、Java 7 Update 17 が公開されていました。ちょっと油断しているといつの間にかアップデートされていて慌てます。今回は JavaFX の方はアップデートされていませんでした。ちなみに Java 6 update 43 もリリースされています。