パーフェクトJavaScriptが届いた

面白そうなんだけど、本が積まれてて読む暇がない。

ProductName パーフェクトJavaScript (PERFECT SERIES 4)
井上 誠一郎
技術評論社 / 3360円 ( 2011-09-23 )


どうしたもんか。

11.09.27 追記

とかいいつつ、ざざっと一気に読んでしまった。これは良書でした。理解が曖昧だったところとかがきっちり書いてあったので理解が深まった。後ろの方のコードは流し読みしてしまったので、あとでちゃんと手を動かす予定。

あとできちんと書く。

HTML5 CANVAS chapter 4

3章は画像の扱い方。アイコンをタイル型に並べた画像ファイルを用意しておいて、任意の部分を切り出してCanavasに貼り付けたりアニメーションさせたりと、動くものをができるのは結構楽しい。

が、この本のサンプル画像が用意されていないのでちょっと困った。

text

ProductName HTML5 Canvas: Native Interactivity and Animation for the Web
Steve Fulton
Oreilly & Associates Inc / 2922円 ( 2011-05-13 )


Express+jade+coffeeで書いたのはGitHubにあげている。

ドット絵ブームが来る予感がしてきた。

Ninety-Nine Scala Problems (P11 - P20)

S-99を解く。List.makeを使ったらdeprecation warningsがでたので、fillにしてみたんだけどこれでいいのだろうか?

scala> List.make(5,3)
warning: there were 1 deprecation warnings; re-run with -deprecation for details
res5: List[Int] = List(3, 3, 3, 3, 3)

pythonのenumerateはscalaではzipWithIndexでこれを使えばp16は

ls.zipWithIndex filter { v => (v._2 + 1) % n != 0 } map { _._1 }

splitAtメソッド使えばリストを2つに分割(P17)できるが、僕はtakeとdrop使ってた。

//11
def pack[A](ls:List[A]): List[List[A]] = ls match {
  case Nil => Nil
  case head::tail => ls.takeWhile(_ == head) :: pack(tail.dropWhile(_ == head))
}

def encodeModified[A](ls: List[A]): List[Any] = pack(ls) map {
  e => if (e.length == 1) e.head else (e.length, e.head)
}

println("s11: " + encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))

//12
//scala> List.make(5,3)
//warning: there were 1 deprecation warnings; re-run with -deprecation for details
//res5: List[Int] = List(3, 3, 3, 3, 3)

def decode[A](ls:List[(Int, A)]): List[A] = ls flatMap {e => List.fill(e._1)(e._2)}

println("s12: " + decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e))))

//13
def encodeDirect[A](ls:List[A]): List[(Int, A)] = ls match {
  case Nil => Nil
  case head::tail => (ls.takeWhile(_ == head).length, head) :: encodeDirect(tail.dropWhile(_ == head))
}

println("s13: " + encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))

//14
def duplicate[A](ls:List[A]): List[A] = ls match {
  case Nil => Nil
  case h::t => h::h::duplicate(t)
}

println("s14: " + duplicate(List('a, 'b, 'c, 'c, 'd)))

//15
def duplicateN[A](n:Int, ls:List[A]): List[A] = ls flatMap {e => List.fill(n)(e) }

println("s15: " + duplicateN(3, List('a, 'b, 'c, 'c, 'd)))

//16
//  // Functional.
//  def dropFunctional[A](n: Int, ls: List[A]): List[A] = 
//    ls.zipWithIndex filter { v => (v._2 + 1) % n != 0 } map { _._1 }
//}

def drop[A](n:Int, ls:List[A]): List[A] = {
  def drop2[A](n:Int, ls:List[A]): List[A] = ls match {
    case Nil => Nil
    case _ => ls.take(n-1) ::: drop2(n, ls.drop(n))
  }
  drop2(n,ls)
}

println("s16: " + drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))

//17
// Builtin.
//  def splitBuiltin[A](n: Int, ls: List[A]): (List[A], List[A]) = ls.splitAt(n)

def split[A](n:Int, ls:List[A]): (List[A],List[A]) = (ls.take(n), ls.drop(n))

println("s17: " + split(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))

//18
def slice[A](s:Int, e:Int, ls:List[A]): List[A] = ls.take(e).drop(s)

println("s18: " + slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))

//19
def rotate[A](n:Int, ls:List[A]): List[A] = {
  if (n >= 0)  ls.drop(n) ::: ls.take(n)
  else ls.drop(ls.length + n) ::: ls.take(ls.length + n)
}

println("s19: " + rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
println("s19: " + rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))

//20
def removeAt[A](n:Int, ls:List[A]): (List[A],A) = {
  val v = ls.splitAt(n)
  (v._1 ::: v._2.tail, v._2.head)
}

println("s20: " + removeAt(1, List('a, 'b, 'c, 'd)))

GAMESSで励起状態の構造最適化をする

光異性化とかAMES予測とか代謝予測とか、量子化学計算が創薬シーンで果たす役割が大きくなっているのは、探索創薬自体が発見学というよりはメカニズムベースでモノを考えるようになってきているからかなぁと。それからFMOなんかも重要な技術ですね。

さて、ちょっと励起状態での構造最適化計算が必要になったので、pygamessでCIS計算できるようにしておきました。

test用にGAMESSのEXAM34のホルムアルデヒドの励起状態計算のサンプルを使っています。GAMESSで計算する場合にはpc-chem.infoのホルムアルデヒドの励起状態計算が参考になります。こういう良質なコンテンツもっと増えてくんないかな。

input用のmol

exam34_energy.log
 OpenBabel09231115413D

  4  3  0  0  0  0  0  0  0  0999 V2000
    0.0100   -0.8670    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.0000    0.3455    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0100    0.9296   -0.9377 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0100    0.9296    0.9377 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  2  0  0  0  0
  2  4  1  0  0  0  0
  3  2  1  0  0  0  0
M  END

計算用スクリプト

import pygamess
import openbabel as ob
g = pygamess.Gamess()
obc = ob.OBConversion()
obc.SetInAndOutFormats("mol","mol")
mol = ob.OBMol()
obc.ReadFile(mol, "h2co.mol")
g.contrl['cityp'] = 'cis'
g.run_type('optimize')
g.basis_type('631+gdp')
g.gamess_input(mol)
newmol = g.run(mol)
print newmol.GetEnergy()
obc.WriteFile(newmol,'h2co_singlet.mol')

励起状態のホルムアルデヒドの安定構造は若干ピラミッド型の構造を取るって知ってた?

h2co

ところで、光異性化のサンプルとして面白いのはやっぱスチルベンかなぁと思ったんだけど、HOMO-LUMOの2電子励起を考慮しないといけないらしいので、CISじゃ計算できないじゃんと。

もう少し面白いサンプルないかなぁ。

Ninety-Nine Scala Problems (P01 - P10)

S-99を解く。Scalaは基本は関数型で考えて、どうしようもないときにはOOPに逃げられるのがいいですね。

//1
def last[A](ls:List[A]): A = ls.last

println("s1: " + last(List(1, 1, 2, 3, 5, 8)))

//2
def penultimate[A](ls:List[A]): A = ls.init.last

println("s2: " + penultimate(List(1, 1, 2, 3, 5, 8)))

//3
def nth[A](n:Int, ls:List[A]): A = if (n == 0) ls.head else nth(n-1,ls.tail)

println("s3: " + nth(2, List(1, 1, 2, 3, 5, 8)))

//4
def length[A](ls:List[A]): Int = ls.length

println("s4: "+ length(List(1, 1, 2, 3, 5, 8)))

//5
def reverse[A](ls:List[A]): List[A] = {
  def reverse2[A](ls:List[A],ls2:List[A]): List[A] = ls match {
      case Nil => ls2 
      case (head::tail) => reverse2(tail,head::ls2)
    }
  reverse2(ls,Nil)
}
println("s5: " + reverse(List(1, 1, 2, 3, 5, 8)))

//6
def isPalindrome[A](ls:List[A]): Boolean = ls == reverse(ls)

println("s6: " +  isPalindrome(List(1, 2, 3, 2, 1)))

//7
def flatten(ls: List[Any]): List[Any] = ls flatMap {
  case ms:List[_] => flatten(ms)
  case e => List(e)
}

println("s7: " + flatten(List(List(1, 1), 2, List(3, List(5, 8)))))

//8
def compress[A](ls:List[A]): List[A] = ls match {
  case Nil    => ls
  case head::tail => head::compress(tail.dropWhile(_ == head))
}

println("s8: " + compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))

//9
def pack[A](ls:List[A]): List[List[A]] = ls match {
  case Nil => Nil
  case head::tail => ls.takeWhile(_ == head) :: pack(tail.dropWhile(_ == head))
}

println("s9: " + pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))

//10
def encode[A](ls: List[A]): List[(Int, A)] = pack(ls) map {e => (e.length, e.head)}

println("s10: " + encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))

「はじめて読む486」を読んでいる

Linuxカーネル2.6解読室の流れで読み始めた。

ProductName はじめて読む486―32ビットコンピュータをやさしく語る
蒲地 輝尚
アスキー / 2548円 ( 1994-09 )


Linuxも色々勉強しておかないといけない。

ProductName Linuxカーネル2.6解読室
高橋浩和
ソフトバンククリエイティブ / 5670円 ( 2006-11-18 )


そしてこれはまだ読んでいない、積んである。

HTML5 CANVAS chapter 3

3章はテキストの扱い方。プロパティをインタラクティブにいじれるものを作りながらText属性に関して学んでいく感じ。これといって難しい所はなくて、さくっとこなした。

text

ProductName HTML5 Canvas: Native Interactivity and Animation for the Web
Steve Fulton
Oreilly & Associates Inc / 2922円 ( 2011-05-13 )


Express+jade+coffeeで書いたのはGitHubにあげている。

ベルマンフォード法とダイクストラのアルゴリズム

プログラミングコンテストチャレンジブックから

ProductName プログラミングコンテストチャレンジブック
秋葉 拓哉
毎日コミュニケーションズ / 3444円 ( 2010-09-11 )


ベルマンフォード

高々|V|-1回で最安定経路にたどり着くっていうのがなかなか理解できなくて、紙に書きながら理解した。

edge = [[0,1,2],[0,2,5],[1,2,4],[1,3,6],[1,4,10],[2,3,2],[3,5,1],[4,5,3],[4,6,5],[5,6,9],
       [1,0,2],[2,0,5],[2,1,4],[3,1,6],[4,1,10],[3,2,2],[5,3,1],[5,4,3],[6,4,5],[6,5,9]]

def shortest_path(edge,num_v,start):
    inf = float("inf")
    d = [inf for f in range(num_v)]
    d[start] = 0;
    while True:
        update = False
        for e in edge:
            if d[e[0]] != inf and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                update = True
        if not update:
            break
    return d

print shortest_path(edge,7,0)[6]

ダイクストラ

こっちは馴染み深い

edge = [[0,1,2],[0,2,5],[1,2,4],[1,3,6],[1,4,10],[2,3,2],[3,5,1],[4,5,3],[4,6,5],[5,6,9],
       [1,0,2],[2,0,5],[2,1,4],[3,1,6],[4,1,10],[3,2,2],[5,3,1],[5,4,3],[6,4,5],[6,5,9]]

def dijkstra(edge,num_v,start):
    inf = float("inf")
    cost = [[inf for i in range(num_v)] for j in range(num_v)]
    for e in edge: cost[e[0]][e[1]] = e[2]
    used = [False for i in range(num_v)]
    d = [inf for j in range(num_v)]

    d[start] = 0;
    while True:
        v = -1
        for u in range(num_v):
            if (not used[u]) and (v == -1 or d[u] < d[v]):
                v = u
        if v == -1: break
        used[v] = True
        for x in range(num_v):
            d[x] = d[x] if d[x] < d[v] + cost[v][x] else d[v] + cost[v][x]
        print d
    return d[num_v-1]

print dijkstra(edge,7,0)

最短経路の本もいいっすね。数学ガールとともに子供の為にとってある。

ProductName 最短経路の本
R. ブランデンベルク
シュプリンガー・ジャパン株式会社 / 3675円 ( 2007-12-13 )


今日の畑(110919)

苗木屋に行ってにんにくを調達したついでに、今から植えるのになにがいいかなと質問したら、人参植えとけって言われたので、ひとみ五寸っていう種類を買って早速撒いてきた。 (次の日雨降ってどうしようもないので、昨日のうちに種まいておいて正解だった。)

レーキが欲しかったのでついに購入。

1316514967

モロヘイヤは成長が鈍ってきた。そろそろ終了かも。

1316514970

おくらは思ったより収穫量が少なかった。来年はもっと早くから種をまこう。

1316514972

ゴーヤは種取り用に少し残してある。熟したのを家に持って帰ると虫が入ってたりして、嫌がられるので、このままにしておいて、地面にこぼれた種を拾うのが楽でいい。

1316514974

あと、ゴーヤを処分したいときには、根っこを引きぬいて何日かほうっておくと、良い感じに枯れてくれるので 処分が楽ですね。

御殿場アートクラフトフェア2011

去年に引き続き今年も御殿場アートクラフトフェアに行ってきた。

今年は娘のワークショップがメインだった。

クラッカー作ったり、

1316412938 1316412940

サノユカシさんとこでバッグに絵描いたりしてきた。

1316412942 1316412944

お約束の風車。秋晴れが気持ちのいいクラフトフェアであった。

1316412946 1316412948