ublasのouter_prodの計算式

boostのublasにはouter_prodというベクトルとベクトルの転置の積を計算する関数が存在する。 どちらのベクトルが転置されているかちゃんと書いたものがなかったのでメモ。 直積の演算としては直感通りの式。 $$ x = \left( \begin{array}{c} x_{1} \\ x_{2} \\ \end{array} \right), y = \left( \begin{array}{c} y_{1} \\ y_{2} \\ \end{array} \right) $$ $$ \mathrm{outer\_prod}(x, y) = xy^{T} = \left( \begin{array}{cc} x_{1}y_{1} & x_{1}y_{2} \\ x_{2}y_{1} & x_{2}y_{2} \\ \end{array} \right) $$

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>

namespace ublas = boost::numeric::ublas;
int main(int argc, char const* argv[])
{
    ublas::vector<double> x(2);
    x(0) = 1.0;
    x(1) = 2.0;
    ublas::vector<double> y(2);
    y(0) = 3.0;
    y(1) = 4.0;

    ublas::matrix<double> A = ublas::outer_prod(x, y);
    std::cout << A(0, 0) << ", " << A(0, 1) << std::endl;//3, 4
    std::cout << A(1, 0) << ", " << A(1, 1) << std::endl;//6, 8

    return 0;
}

数式が使える最強のプレゼンテーション環境

うれしい

  • markdownでかける
  • webで見れる
  • githubで管理できる
  • MathJaxでlatexの数式が使える
    • markdownとMathJaxの組み合わせで起こりがちの_などのエスケープ問題を回避できる。

使うもの

  • Remark.js
    • github
    • jsをurlで読み込めるので、ネットにつながる環境ならすごく便利。
  • git, github
  • MathJax

サンプル

導入手順

  1. ローカルに適当にディレクトリ(slidesとする)を作る。
  2. slidesディレクトリにindex.htmlを作り、同じディレクトリにcontents.mdを配置する。
  3. index.htmlをブラウザで開けばOKスライドが見れる。 index.html
<DOCTYPE html>
<html>
<head><title>Presentation</title></head>
<body>
  <script src="https://remarkjs.com/downloads/remark-latest.min.js" type="text/javascript"></script>
  <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript"></script>
</script>
  <script type="text/javascript">
    var slideshow = remark.create({
      sourceUrl: "contents.md"
    });

    // Setup MathJax
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
      }
    });
    MathJax.Hub.Queue(function() {
      $(MathJax.Hub.getAllJax()).map(function(index, elem) {
        return(elem.SourceElement());
      }).parent().addClass('has-jax');
    });
    MathJax.Hub.Configured();
  </script>
</body>
</html>

contents.md

# slide 1 
- 1ページ目
---

# slide 2
- 2ページ目

$$
x = a_{1} + b^{2}
$$
## フーリエ変換
$$
  F(u) = \int\_{-\infty}^{\infty} f(x)\mathrm{e}^{-j2\pi ux}dx
$$

## 2次元のフーリエ変換
$$
  F(u,v) = \int\_{-\infty}^{\infty} \int\_{-\infty}^{\infty} f(x,y)\mathrm{e}^{-j2\pi (ux + vy )}dxdy
$$

ここまでで、ローカルでスライドが見れる環境ができる。 次は、github pagesを作って、github上でスライドが見れるようにする。

 4. slides上でgitレポジトリを作り、全部コミット。

git init
git add .
git commit -m 'Initial commit.'

 5. githubにレポジトリを作り、slidesのレポジトリをpushする。

git remote add https://github.com/username/repository_namae/
git push origin master

 6. gh-pagesを使う為、gh-pagesブランチを作って、githubにpushする。

git checkout -b gh-pages
git push origin gh-pages

 7. https://username.github.io/repository_nameにアクセスするとレポジトリトップのindex.htmlのファイルが表示される。

  • uploadしたデータが反映されるまで10分ほどかかるみたいなので気長にまつ。
  • usernamegithubのユーザ名
  • reposiotry_nameはレポジトリの名前

キー操作

  • j 次のスライド
  • k 前のスライド
  • c スライドのclone
    • スライドを clone すると、元のスライドでページ移動した際、clone 先のスライドも同期して移動する。
  • f フルスクリーン
  • p プレゼンモード
  • h ヘルプの表示

補足

  • contents.mdでは、普通にmarkdown中にmathjaxを書けばOK。 具体的には、$$で数式を囲む。
  • mathjaxのURLにdelayStartupUntil=configuredオプションを含めると、markdownで問題になる_エスケープなどが不要になるので便利。

個人的には、スライドごとにレポジトリを作る気分にあんまりならないので、下記のように1つのレポジトリをまとめている。 特に工夫はないが、レポジトリのトップのcontents.mdはレポジトリ内のスライドへのリンクテーブルになっている個人的に便利。

サンプル

  1. githubにレポジトリを作る。
  2. レポジトリのトップに、index.htmlcontetns.mdを置く。contents.mdはサブディレクトリへのリンク。
# link
* [directory_name](http://user_name.github.io/repository_name/directory_name/)
  * sample

 3. レポジトリにスライドごとにディレクトリを作り、各ディレクトリにindex.htmlcontents.mdを作る。

参考

markdown + remark.js + gh-pages でプレゼン資料を公開する

NeoBundleCleanが使えない

NeoBundleCleanはdangerousなので削除したとのこと。

How to Uninstall Packages? · Issue #356 · Shougo/neobundle.vim · GitHub

プラグインを読み込まないようにしたい場合は、NeoBundle hoge/fuga.vimの記述を消せば読み込まれないとのこと。

プラグインを削除したい場合は、

call neobundle#begin(expand('~/.vim/bundle/'))

で指定しているディレクトリからpluginのディレクトリを手動で削除する。

windowsでgithubのrepositoryにpushするときにエラーがでる場合の対応

使用しているgitは以下。

$ git version
git version 2.6.3.windows.1

次のコマンドを実行すると、

git remote add origin https://hogehoge.gihttps://github.com/user_id/hogehoge.git
git push origin master

下記のエラーがでる。

!!!bash: /dev/tty: No such device or address!!!
!!!error: failed to execute prompt script (exit code 1)!!!
!!!fatal: could not read Username for 'https://github.com': Invalid argument!!!
!!!vimshell: exit 128 "git push origin master"!

上記はvimshellでのエラーだが、cmdから直接実行しても同様のエラーがでる。

対処法?

 1. 以下の方法によるが、試してない。 [Windows] bash: /dev/tty: No such a device or address · Issue #8984 · atom/atom · GitHub

以下のURLをremote repositoryのURLとして設定する。

https://<username>:<password>@github.com/<username>/<repo_name>.git

たとえば、次の場合、 * username:hoge * password:fuga * repo_name:hage

git remote add origin https://hoge:fuga@github.com/hage/hage.git

 2. こちらは、cmd上で動作は確認。vimshell上では未確認。

https://hoge@github.com/hage/hage.git

とするとパスワードが求められるので、入力する。

Googleの検索結果画面で使えるショートカット

Firefoxでは使えないようだが、chrome及びIEでは使える検索結果画面でのショートカット。

Google

googleのホームから適当にワードを検索する。

検索結果画面で以下のショートカットが使える。

キーボード 機能
J 次の検索結果を選択
K 次の検索結果を選択
O 選択した結果を表示
Enter 選択した結果を表示
/ 検索バーにフォーカス
Esc 検索バーのフォーカスをはずす

cppunitのテンプレ

cppunitでテストコードを書くときのテンプレ。

まずは、main.cpp。これはまるこぴでおっけ。

#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>


int main(int argc, char* argv[])
{
    // Create the event manager and test controller
    CPPUNIT_NS::TestResult controller;

    // Add a listener that colllects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener(&result);

    // Add a listener that print dots as test run.
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener(&progress);

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
    runner.run(controller);

    // Print test in a compiler compatible format.
    CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
    outputter.write();

    {
        int a = 0;
        std::cin >> a;
    }

    return result.wasSuccessful() ? 0 : 1;
}
    

テスト対象のクラスがNormalRandomGeneratorとする。

テスト用のクラスとしてNormalRandomGeneratorTest.h及びNormalRandomGeneratorTest.cppを用意する。

テストクラスのsetUp及びtearDownメソッドは各テストメソッド(ここではtestOperatorParenthesis)を実行する前と後に実行されるメソッド

NormalRandomGeneratorTest.h

#pragma once

//include test target hearder.
#include "cva/random/NormalRandomGenerator.h"

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
namespace cva_test {
    class NormalRandomGeneratorTest : public CPPUNIT_NS::TestFixture {
        CPPUNIT_TEST_SUITE(NormalRandomGeneratorTest); //class name
        CPPUNIT_TEST(testOperatorParenthesis); //nane of test
        CPPUNIT_TEST_SUITE_END();

    private:
        void testOperatorParenthesis(); //test metdho declaration

        NormalRandomGenerator *_generator;
    };
}

NormalRandomGeneratorTest.cpp

#include "NormalRandomGeneratorTest.h"
namespace cva_test {
    CPPUNIT_TEST_SUITE_REGISTRATION(NormalRandomGeneratorTest);

    void NormalRandomGeneratorTest::setUp()
    {
        _generator = new NormalRandomGenerator();
    }

    void NormalRandomGeneratorTest::tearDown()
    {
        delete _generator;
    }

    void NormalRandomGeneratorTest::testOperatorParenthesis()
    {
        //test here
    }
}

参考

テスト駆動開発とかよくわからない人のための今すぐ使えるCppUnitテンプレ - EchizenBlog-Zwei

Visual Studio C++でCppUnitをビルドできない、そんなとき | skmks