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
340
Clojureで多次元配列
ClojureでJavaの配列を利用して数値計算を扱う。
ichisemasashi
December 23, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
코틀린으로 멀티플랫폼 만들기
pangmoo
0
120
プールにゆこう
irof
2
120
StreamlitとTerraformでデータカタログを作った話
gussan0223
0
300
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
300
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
39
18k
両面どころかインフラもTSでできるよ ~ 全方位TypeScriptによるプロダクト開発 ~
myfinder
9
3.2k
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
0
110
"config" ってなんだ? / What is "config"?
okashoi
0
220
元気予報
suu_mire0726
0
860
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
250
コードレビューで学ぶ!Kotlinオブジェクト指向デザインパターン
akkie76
2
180
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
370
Featured
See All Featured
Unsuck your backbone
ammeep
662
57k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Scaling GitHub
holman
457
140k
Web development in the modern age
philhawksworth
202
10k
Agile that works and the tools we love
rasmusluckow
324
20k
Optimizing for Happiness
mojombo
370
69k
What's in a price? How to price your products and services
michaelherold
237
11k
Code Reviewing Like a Champion
maltzj
513
39k
Building a Modern Day E-commerce SEO Strategy
aleyda
16
6.4k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
658
120k
A Tale of Four Properties
chriscoyier
150
22k
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))