プログラミングとかLinuxとかの備忘録

プログラミング、Linuxでハマった箇所や環境構築のメモ

virtualenvのインストール方法

virtualenvをインストールする方法 CentOS7,Fedora21で確認済み Fedora22で行う場合はyumdnfに置き換えて実行してください

Python, pipのインストール

$ sudo yum -y install python-devel
$ sudo yum -y install python-setuptools

$ sudo easy_install pip
$ sudo pip install -U pip

virtualenv

$ sudo pip install virtualenv

$ mkdir ~/.pyenv
$ cd ~/.pyenv

virtualenvの使い方 (基本的には使わない)

command description
$ python -m virtualenv ${PYENV_NAME} 仮想環境作成
$ rm -rf ${PYENV NAME} 仮想環境削除
$ source ${PYENV_DIR}/bin/activate 仮想環境起動
(PYENV_NAME)$ deactivate 仮想環境終了

virtualenvwrapper

インストール

$ sudo pip install virtualenvwrapper

ログインシェルを編集

$ which virtualenvwrapper.sh
/usr/bin/virtualenvwrapper.sh

$ vi ~/.bashrc
### Virtualenvwrapper
if [ -f /usr/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.pyenv
    source /usr/bin/virtualenvwrapper.sh
fi

virtualenvwrapperの使い方

command description
$ workon 仮想環境一覧
$ mkvirtualenv --no-site-packages ${PYENV_NAME} 仮想環境作成(システムパッケージを無視)
$ mkvirtualenv -python=/path/to/python ${PYENV_NAME} 仮想環境作成(Pythonのバージョンを指定)
$ mkvirtualenv ${PYENV_NAME} 仮想環境作成(デフォルトのPythonを使用)
$ rmvirtualenv ${VENV_NAME} 仮想環境削除
$ workon ${VENV_NAME} 仮想環境に入る

HTML属性に値を設定する

element.setAttribute(element_name, value)を使用する

ページ内のリンクをすべてnew_urlに変更するサンプル

var new_url = "http://xxx.yyy";

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)
{
    links[i].setAttribute("href", new_url);
}

上の例の場合はsetAttribute()を使わずに

links[i].href = new_url;

とかける.

setAttribute()を使うメリットがあるのか??

HTMLからオブジェクトを取得する

GetElementsBy~を使ってHTMLからオブジェクトを取得する方法

HTMLタグ名を指定

document.getElementsByTagName()を使用する
引数にはタグ名を文字列で渡す

// リンクをすべて取得
var links = document.getElementsByTagName("a");

for (var i = 0; i < links.length; i++)
{
    // リンクURLを取得
    var link_url = links[i].href;
}

id属性を指定

document.getElementById()を使用する
引数にはidを文字列で渡す

これだけGetElementsBy~ではなく,sが必要ない
返値が配列ではないので注意が必要

var ids = document.getElementById("id");

class属性を指定

document.getElementsByClassName()を使用する
引数にはクラス名を文字列で渡す

var cls = document.getElementsByClassName("class_name");

name属性を指定

document.getElementsByName()を使用する
引数にはname属性の値を文字列で渡す

var names = document.getElementsByName("name_value");

JAVAでQuaternionを使う

はじめに

JAVAではjavax.vecmath.Quat4dでQuaternionを使える
C++boost::math::quaternionと同じ感覚で使ってちょっとハマったのでメモ

インスタンス

Quat4d quaternion = new Quat4d(x, y, z, w);

で行える
この場合は(x, y, z)がそのままの値ではなく、正規化された値になるので、そのままの値でQuat4dを作成したい場合は関数を作成すると便利

public static Quat4d genQuat4d(final double x, final double y, final double z, final double w) {
    Quat4d tmp = new Quat4d();
    tmp.setX(x);
    tmp.setY(y);
    tmp.setZ(z);
    tmp.setW(w);

    return tmp;
}

回転

任意の点の回転は上の関数を用いて以下のようにする

Quat4d point = genQuat4d(x, y, z, 0);       // 回転させたい座標
Quat4d rot = new Quat4d(xr, yr, zr, wr);    // 回転クォータニオンを作成
Quat4d conj = new Quat4d(rot);

conj.conjugate();
conj.mul(point);
conj.mul(rot);

point.setX(conj.x);
point.setY(conj.y);
point.setZ(conj.z);

サンプルコード

import javax.vecmath.Vector3d;
import javax.vecmath.Quat4d;

public class Quaternion {
    public static Quat4d gen(final double x, final double y, final double z, final double w) {
        Quat4d tmp = new Quat4d();
        tmp.setX(x);
        tmp.setY(y);
        tmp.setZ(z);
        tmp.setW(w);

        return tmp;
    }
    public static Quat4d gen(final double angle, final Vector3d axis) {
        double sin2 = Math.sin(angle / 2.0);
        axis.normalize();
        return gen(axis.x*sin2, axis.y*sin2, axis.z*sin2, Math.cos(angle / 2.0));
    }
    public static Quat4d gen(final Vector3d axis) {
        return gen(axis.x, axis.y, axis.z, 0);
    }


    public static Quat4d rotate(final Quat4d src, final Quat4d rot) {
        Quat4d tmp = new Quat4d(rot);
        tmp.conjugate();
        tmp.mul(src);
        tmp.mul(rot);

        return tmp;
    }
    public static Quat4d rotate(final Vector3d src, final Quat4d rot) {
        return rotate(gen(src), rot);
    }
    public static Quat4d rotate(final double x, final double y, final double z, final Quat4d rot) {
        return rotate(new Vector3d(x, y, z), rot);
    }
}