Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Clojureで多次元配列
Search
ichisemasashi
December 23, 2020
Programming
0
440
Clojureで多次元配列
ClojureでJavaの配列を利用して数値計算を扱う。
ichisemasashi
December 23, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
200
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
960
エンジニアインターン「Treasure」とHonoの2年、そして未来へ / Our Journey with Hono Two Years at Treasure and Beyond
carta_engineering
0
250
CSC509 Lecture 04
javiergs
PRO
0
300
TFLintカスタムプラグインで始める Terraformコード品質管理
bells17
2
200
Introducing ReActionView: A new ActionView-Compatible ERB Engine @ Kaigi on Rails 2025, Tokyo, Japan
marcoroth
3
1k
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
850
kiroとCodexで最高のSpec駆動開発を!!数時間で web3ネイティブなミニゲームを作ってみたよ!
mashharuki
0
600
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
200
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
400
CSC509 Lecture 05
javiergs
PRO
0
300
CSC305 Lecture 06
javiergs
PRO
0
240
Featured
See All Featured
Building an army of robots
kneath
306
46k
Automating Front-end Workflow
addyosmani
1371
200k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Thoughts on Productivity
jonyablonski
70
4.9k
Mobile First: as difficult as doing things right
swwweet
224
10k
The World Runs on Bad Software
bkeepers
PRO
72
11k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
620
The Pragmatic Product Professional
lauravandoore
36
6.9k
Transcript
@ichisemasashi 202012݄23 ClojureͰଟ࣍ݩྻ
࠷ۙؾ͍ͨɺ͕ࣗίϯϐϡʔλʔͰΓ͔ͨͬͨ͜ͱ => ʮܭࢉʯ - ͱΓ͋͑ͣɺJuliaΛͬͯษڧΛ͍͕ͯͨ͠ɺͰ͖ΕClojureͰษڧ Λ͔ͨͬͨ͠ɻ - ͰClojureʹଟ࣍ݩྻ͕ͳ͍ͱࢥ͍ͬͯͨɻ -> JavaͷྻΛ͑ྑ͍ɻ
-> tnoda͞ΜͷʮJava ͷྻΛར༻ͯ͠ϝϞϦͱ࣌ؒͷนΛಥ ഁ͢ΔɽʯʹɺJavaͷྻͷ͍ํ͕ཧ͞Ε͍ͯͨɻ
ClojureͰJavaͷྻΛར༻͢Δ ྻͷੜ user=> (def my-array (make-array Integer/TYPE 2 2)) #'user/my-array
ClojureͰJavaͷྻΛར༻͢Δ ྻͷදࣔ user=> (pprint my-array) [[0, 0], [0, 0]] nil
ClojureͰJavaͷྻΛར༻͢Δ ྻͷཁૉΛࢀর user=> (aget ^ints my-array 0 0) 0
ClojureͰJavaͷྻΛར༻͢Δ ྻͷཁૉʹσʔλΛઃఆ user=> (aset ^ints my-array 0 0 (int 100))
100 user=> (aget ^ints my-array 0 0) 100 user=> (pprint my-array) [[100, 0], [0, 0]] nil
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ֓ཁ 5x + y + z = 10 x
+ 4y + z = 12 2x + y + 3z = 13 }
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ֓ཁ 5x + y + z = 10 x
+ 4y + z = 12 2x + y + 3z = 13 } x = (10 − y − z)/5 y = (12 − x − z)/4 z = (13 − 2x − y)/3 }
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ֓ཁ 5x + y + z = 10 x
+ 4y + z = 12 2x + y + 3z = 13 } x = (10 − y − z)/5 y = (12 − x − z)/4 z = (13 − 2x − y)/3 } x y z ॳظ 1 1 1 1८ 1.600 2.350 2.483 2८ 1.033 2.121 2.938 3८ 0.988 2.019 3.002 4८ 0.996 2.001 3.002 5८ 0.999 1.999 3.001
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ࣮ (def an-eq (make-array Double/TYPE 3 4)) (def N
(alength an-eq)) (defn set-eq [arr n x y z w] (aset ^doubles arr n 0 (double x)) (aset ^doubles arr n 1 (double y)) (aset ^doubles arr n 2 (double z)) (aset ^doubles arr n 3 (double w))) (set-eq an-eq 0 5 1 1 10) (set-eq an-eq 1 1 4 1 12) (set-eq an-eq 2 2 1 3 13) (pprint an-eq)
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ࣮(ଓ) (def res-arr (make-array Double/TYPE 3)) (dotimes [i N]
(aset ^doubles res-arr i (double 1))) (pprint res-arr) (def tmp-arr (make-array Double/TYPE 3)) (pprint tmp-arr) (defn eq-elm [x y] (aget ^doubles an-eq x y)) (defn res-elm [x] (aget ^doubles res-arr x)) (defn tmp-elm [x] (aget ^doubles tmp-arr x))
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ࣮(ଓ) (def MAX_LOOP 30) (def EPS (double 0.0001)) (def
q (atom (double 1.0))) (def ll (atom 0)) (defn display-result [] (dotimes [i N] (let [res (res-elm i)] (println (format "x%d = %9.6f" i res)))))
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ࣮(ଓ) (defn computing [] (let [s (atom (double 0.0))]
(dotimes [i N] (reset! s (double 0.0)) (dotimes [j N] (if (not= i j) (reset! s (+ @s (* (eq-elm i j) (res-elm j)))))) (aset ^doubles tmp-arr i (/ (- (eq-elm i N) @s) (eq-elm i i))) (reset! q (+ @q (Math/abs (- (res-elm i) (tmp-elm i))))) (aset ^double res-arr i (tmp-elm i)))))
࣮ྫɿΨε-βΠσϧ๏ʹΑΔ࿈ཱ1࣍ํఔࣜͷղ๏ Ψε-βΠσϧ๏ͷ࣮(ଓ) (while (and (< EPS @q) (>= MAX_LOOP @ll))
(do (swap! ll inc) (reset! q (double 0.0)) (computing))) (if (>= @ll MAX_LOOP) (println "ऩଋͤͣɻ") (display-result))