Solaris 10 10/08が10月31日に出ました.
http://www.sun.com/aboutsun/pr/2008-10/sunflash.20081031.1.xml
2008年11月5日水曜日
2008年11月1日土曜日
行列のできるプログラミング言語(2)
C++編
■boost::numeric::ublas編
#ifndef __UBLAS_UTIL_HPP__
#define __UBLAS_UTIL_HPP__
#include <iostream>
#include <cmath>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/triangular.hpp>
using namespace std;
using namespace boost;
using namespace boost::numeric;
using namespace boost::numeric::ublas;
typedef ublas::matrix<double> dmatrix;
typedef ublas::matrix<complex<double> > cdmatrix;
typedef ublas::vector<double> dvector;
typedef ublas::vector<complex<double> > cdvector;
/*
matrixの表示
*/
template<class Matrix>
void m_print(Matrix x) {
int n1 = x.size1();
int n2 = x.size2();
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
cout << x(i, j) << "\t";
}
cout << endl;
}
return;
}
/*
vectorの表示
*/
template<class Vector>
void v_print(Vector x) {
int n = x.size();
for (int i = 0; i < n; i++) {
cout << x(i) << endl;
}
return;
}
#endif
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include "uBlas_Util.hpp"
using namespace std;
using namespace boost::numeric::ublas;
int main() {
// make matrix A
dmatrix A(3, 3);
double m[][3] = { { 3, -4, 2 }, { 2, 5, 3 }, { -2, 3, -2 } };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
A(i, j) = m[i][j];
}
}
cout << "matrix A:" << endl;
m_print(A);
// make A^-1
dmatrix I = identity_matrix<double>(3);// 単位行列
dmatrix copyA(A);// Aのコピー
// LU分解時の行交換情報が入る行列
permutation_matrix<> pm(A.size1());
// LU分解
lu_factorize(A,pm);
// 行交換を考慮した前進消去と後退代入
lu_substitute(A,pm,I);
// 解の表示
cout << "A^-1" << endl;
m_print(I);
// A*A^-1
cout << "A*A^-1" << endl;
m_print(prod(copyA,I));
return 0;
}
行列のできるプログラミング言語(1)
いつか何かの役に立つかも知れないのでメモ.
■JAMA編
Java編
■Jakarta commons math
import java.math.BigDecimal;
import org.apache.commons.math.linear.*;
public class Main {
public static void main(String[] args) {
// 行列Aを作成
double[][] m = {{3,-4,2}, {2,5,3}, {-2,3,-2}};
BigMatrix A = MatrixUtils.createBigMatrix(m);
System.out.println("行列A");
printBigMatrix(A);
// Aの逆行列A^-1を求める
BigMatrix inA = A.inverse();
System.out.println("行列A^-1");
printBigMatrix(inA);
// AとA^-1の積
BigMatrix tmp = A.multiply(inA);
System.out.println("A*A^-1");
printBigMatrix(tmp);
// 連立方程式
// Ax = b
BigDecimal [] b =new BigDecimal [3];
double [] t = {0,6,1};
for(int i=0;i<3;i++){
b[i]= new BigDecimal(t[i]);
}
BigDecimal[] x = A.solve(b);
System.out.println("連立方程式");
for(int i=0;i<3;i++){
System.out.println(x[i]);
}
return;
}
public static void printBigMatrix(BigMatrix tmp){
for (int i = 0; i < tmp.getRowDimension(); i++) {
for(int j=0;j<tmp.getColumnDimension();j++){
System.out.print(tmp.getEntry(i, j) + "\t");
}
System.out.print("\n");
}
}
}
■JAMA編
import Jama.*;
public class Main {
public static void main(String[] args) {
double[][] m = {{3,-4,2}, {2,5,3}, {-2,3,-2}};
Matrix A = new Matrix(m);
double [] t = {0,6,1};
Matrix b = new Matrix(3,1);
for(int i=0;i<3;i++)
b.set(i, 0, t[i]);
Matrix x = A.solve(b);
for(int i = 0;i<x.getRowDimension();i++)
System.out.println(x.get(i, 0));
}
}
2008年10月24日金曜日
Java3Dを使おう(2)
Java3DでSunSPOTを3Dで作成しようと現在勉強中(ってほど時間かける余裕ないけど)
現在までの成果はこんな感じ..
日本語のまとまったドキュメントが欲しいです...なんか本でも買おうかな(笑)

ついでにソースリスト.
現在までの成果はこんな感じ..
日本語のまとまったドキュメントが欲しいです...なんか本でも買おうかな(笑)
ついでにソースリスト.
package java3dsuspot;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import javax.swing.*;
import java.awt.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;
/**
* Java3DでSunSPOTを作成
* @author taka
*/
public class Spot3D extends JFrame{
private TransformGroup objTrans1;
private TransformGroup objTrans2;
private SimpleUniverse uni;
private Canvas3D canvas;
private RotationInterpolator rotatX;
private RotationInterpolator rotatY;
private RotationInterpolator rotatZ;
private Alpha alpha;
/**
* コンストラクタ
*/
public Spot3D(){
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
getContentPane().add(canvas, BorderLayout.CENTER);
BranchGroup scene = createSceneGraph();
uni = new SimpleUniverse(canvas);
uni.getViewingPlatform().setNominalViewingTransform(); // 視点の設定
uni.addBranchGraph(scene);
setOrbitBehavior();
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* 3Dオブジェクトの作成
*/
public BranchGroup createSceneGraph() {
// ObjectTreeの根の用意
BranchGroup objRoot = new BranchGroup();
// 座標軸を追加
Point3d[] vertex = new Point3d[6];
vertex[0] = new Point3d(-10.0, 0.0, 0.0); vertex[1] = new Point3d(10.0, 0.0, 0.0);
vertex[2] = new Point3d(0.0, -10.0, 0.0); vertex[3] = new Point3d(0.0, 10.0, 0.0);
vertex[4] = new Point3d(0.0, 0.0, -10.0); vertex[5] = new Point3d(0.0, 0, 10.0);
LineArray geometry = new LineArray(vertex.length,
GeometryArray.COORDINATES | GeometryArray.COLOR_3);
geometry.setCoordinates(0, vertex);
geometry.setColor(0, new Color3f(Color.red));
geometry.setColor(1, new Color3f(Color.red));
geometry.setColor(2, new Color3f(Color.cyan));
geometry.setColor(3, new Color3f(Color.cyan));
geometry.setColor(4, new Color3f(Color.blue));
geometry.setColor(5, new Color3f(Color.blue));
Shape3D shape = new Shape3D(geometry);
objRoot.addChild(shape);
// 光源を追加
objRoot.addChild(createLight1());
objRoot.addChild(createLight2());
// 新しい座標系
Transform3D t3d1 = new Transform3D();
Matrix4f mat = new Matrix4f(
1.0f,0.0f,0.0f,0.5f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d1.set(mat);
Transform3D t3d2 = new Transform3D();
Matrix4f mat2 = new Matrix4f(
1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d2.set(mat2);
// transグループの作成
objTrans1 = new TransformGroup(t3d1);// x方向+0.5
objTrans2 = new TransformGroup(t3d2);// 変更なし
// 許可
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
/*
alpha = new Alpha(
1, Alpha.INCREASING_ENABLE, 0, 0, 3000, 400, 0, 3000, 400, 0);
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
//axis.rotX(Math.PI / 2);
RotationInterpolator rotat =
new RotationInterpolator(
alpha, objTrans2, axis,0.0f, (float) Math.PI / 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotat.setSchedulingBounds(bounds);
objTrans2.addChild(rotat);
*/
// key操作
KeyNavigatorBehavior knb = new KeyNavigatorBehavior(objTrans2);
BoundingSphere bounds = new BoundingSphere();
bounds.setRadius(100);
knb.setSchedulingBounds(bounds);
objTrans2.addChild(knb);
// objTreeに追加
objTrans2.addChild(objTrans1);
objRoot.addChild(objTrans2);
// Boxを作成
Appearance ap = new Appearance(); // 物体の質感
Material ma = new Material();
ma.setDiffuseColor(1.0f,1.0f,1.0f); // 色の設定(RGB)
ap.setMaterial(ma);// materialをappearanceにセット
Box box = new Box(0.5f,0.1f,0.3f,ap); // Boxの作成
Box box2 = new Box(0.08f,0.02f,0.3f,ap);
// 木に追加
objTrans2.addChild(box);
objTrans1.addChild(box2);
// コンパイル
objRoot.compile();
return objRoot;
}
/**
* 回転系
*/
public void rotation(){
// x軸
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
axis.rotZ(Math.PI / 2);
rotatX = new RotationInterpolator(
alpha, objTrans2, axis, 0.0f, (float) Math.PI * 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotatX.setSchedulingBounds(bounds);
objTrans2.addChild(rotatX);
}
/**
* 光源を作成
* @return
*/
private Light createLight1() {
// 平行光源
DirectionalLight light = new DirectionalLight(true,
new Color3f(1.0f, 1.0f, 1.0f), // 光源の色
new Vector3f(0.0f, 0.0f, -1.0f));// 光の向き
// AmbientLight light = new AmbientLight(true,new Color3f(1.0f,1.0f,1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲
return light;
}
private Light createLight2() {
AmbientLight light = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲
return light;
}
/**
* マウス操作
*/
public void setOrbitBehavior(){
OrbitBehavior orbit = new OrbitBehavior(canvas,OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds(
new BoundingSphere(new Point3d(0,0,0),100)
);
uni.getViewingPlatform().setViewPlatformBehavior(orbit);
}
}
登録:
投稿 (Atom)