裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

データ収集から可視化、モデリング、デプロイメントまでのエンドツーエンドのデータ分析プロジェクトの実行

2023年09月30日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Vaexという高速なデータフレームライブラリの紹介

2023年09月29日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その447)

2023年09月28日 | Julia

算額(その447)

河□狭山牛頭天王社 林助右衛門自弘 寛政7年乙卯11月(1795) 

藤田貞資(1789):神壁算法巻上
http://www.wasan.jp/jinpeki/jinpekisanpo1.pdf

図のように 12 個の円がある。甲円,乙円,丙円の直径がそれぞれ 43寸7分5厘,51寸0分3厘,141寸7分5厘のとき丁円の直径はいかほどか。

左右対称なので,右半分の図形で方程式を立てる。
甲円の半径と中心座標を r1, (r1, y1)
乙円の半径と中心座標を r2, (r2, y2)
丙円の半径と中心座標を r3, (r3, y3)
丁円の半径と中心座標を r4, (x4, y4)
戊円の半径と中心座標を r5, (r5, y5)
己円の半径と中心座標を r6, (r6, 0)
とおき,連立方程式の解を求める。

include("julia-source.txt")

using SymPy

@syms r1::positive, y1::positive,
     r2::positive, y2::positive,
     r3::positive, y3::positive,
     r4::positive, x4::positive, y4::positive,
     r5::positive, y5::positive,
     r6::positive;

(r1, r2, r3) = (4375, 5103, 14175) .// 200
eq1 = (r1 - r3)^2 + (y1 - y3)^2 - (r1 + r3)^2
eq2 = (r1 - r5)^2 + (y1 - y5)^2 - (r1 + r5)^2
eq3 = (r2 - r3)^2 + (y2 - y3)^2 - (r2 + r3)^2
eq4 = (r6 - r2)^2 + y2^2 - (r2 + r6)^2
eq5 = (r3 - r5)^2 + (y3 - y5)^2 - (r3 + r5)^2
eq6 = (r3 - r6)^2 + y3^2 - (r3 + r6)^2
eq7 = (x4 - r5)^2 + (y4 - y5)^2 - (r4 + r5)^2
eq8 = (r3 - x4)^2 + (y3 - y4)^2 - (r3 + r4)^2
eq9 = (x4 - r6)^2 + y4^2 - (r4 + r6)^2;
res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9], (y1, y2, y3, r4, x4, y4, r5, y5, r6))

   3-element Vector{NTuple{9, Sym}}:
    (1071/8, 5103/40, 1701/8, 189/920, 84861/920, 26649/184, 14175/128, 567/16, 5103/32)
    (1071/8, 5103/40, 1701/8, 6048/215, 18333/860, 43659/344, 2025/224, 162, 5103/32)
    (2331/8, 5103/40, 1701/8, 3267/40, 8883/40, 1863/8, 14175/128, 6237/16, 5103/32)

3 組目のものが適解である。

   r1 = 21.875;  r2 = 25.515;  r3 = 70.875
   y1 = 291.375;  y2 = 127.575;  y3 = 212.625;  r4 = 81.675;  x4 = 222.075;  y4 = 232.875;  r5 = 110.742;  y5 = 389.812;  y6 = 159.469

丁円の直径は 2*3267/40 = 163.35, 163寸3分5厘である。

2*3267/40

   163.35

using Plots

function circle2(x, y, r, color)
   circle(x, y, r, color)
   circle(-x, y, r, color)
end

function draw(more=false)
   pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r1, r2, r3) = (4375, 5103, 14175) .// 200
   (y1, y2, y3, r4, x4, y4, r5, y5, r6) = [305, 138, 224, 85, 225, 252, 106.0, 403, 174.4]
   (y1, y2, y3, r4, x4, y4, r5, y5, r6) = res[3]
   @printf("r1 = %g;  r2 = %g;  r3 = %g\n", r1, r2, r3)
   @printf("y1 = %g;  y2 = %g;  y3 = %g;  r4 = %g;  x4 = %g;  y4 = %g;  r5 = %g;  y5 = %g;  y6 = %g\n", y1, y2, y3, r4, x4, y4, r5, y5, r6)
   @printf("丁円の直径 = %g\n", 2r4)
   plot()
   circle2(r1, y1, r1, :red)
   circle2(r2, y2, r2, :blue)
   circle2(r3, y3, r3, :green)
   circle2(x4, y4, r4, :brown)
   circle2(r5, y5, r5, :magenta)
   circle2(r5, y5, r5, :magenta)
   circle2(r6, 0, r6, :orange)
   if more
       delta = (fontheight = (ylims()[2]- ylims()[1]) / 500 * 10 * 2) /  3  # size[2] * fontsize * 2
       point(r1, y1, "  甲:r1,(r1,y1)", :red, :left, :bottom, delta=delta)
       point(r2, y2, " 乙:r2,(r2,y2)", :black, :left, :vcenter)
       point(r3, y3, " 丙:r3,(r3,y3)", :green, :center, :top, delta=-delta)
       point(x4, y4, " 丁:r4,(r4,y4)", :brown, :center, :bottom, delta=delta)
       point(r5, y5, " 戊:r5,(r5,y5)", :magenta, :center, :bottom, delta=delta)
       point(r6,  0, "己:r6,(r6,0)", :orange, :center, :bottom, delta=delta)
       hline!([0], color=:gray, lw=0.5)
       vline!([0], color=:gray, lw=0.5)
   else
      plot!(showaxis=false)
   end
end;

コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その446)

2023年09月28日 | Julia

算額(その446)

寛政7年乙卯10月(1795) 久留米 鈴木半平正晟
藤田貞資(1807):続神壁算法
http://www.wasan.jp/jinpeki/zokujinpekisanpo.pdf

外円内に甲,乙,丙,丁の 4 種類の円が入っている。それぞれ隣り合う円に外接し,外円に内接している。甲円の直径が 3寸0分5厘のとき,外円の直径はいかほどか。

外円の半径と中心座標を r0, (0, 0)
甲円の半径と中心座標を r1, (0, r0 - r1)
乙円の半径と中心座標を r2, (0, r0 - 2r1 - r2, 0)
丙円の半径と中心座標を r3, (x3, y3)
丁円の半径と中心座標を r4, (r4, 0)
とおき,連立方程式の解を求める。

include("julia-source.txt")

using SymPy

@syms r0::positive, r1::positive,
     r2::positive, r3::positive, x3::positive, y3::positive,
     r4::positive;

r1 = 305//200
r4 = r0//2
eq1 = x3^2 + (r0 - r1 - y3)^2 - (r1 + r3)^2
eq2 = x3^2 + (y3 - r0 +2r1 + r2)^2 - (r2 + r3)^2
eq3 = r4^2 + (r0 - 2r1 - r2)^2 - (r2 + r4)^2
eq4 = (x3 - r4)^2 + y3^2 - (r3 + r4)^2
eq5 = x3^2 + y3^2 - (r0 - r3)^2;
# res = solve([eq1, eq2, eq3, eq4, eq5], (r0, r2, r3, x3, y3));

using NLsolve

function nls(func, params...; ini = [0.0])
   if typeof(ini) <: Number
       r = nlsolve((vout, vin) -> vout[1] = func(vin[1], params..., [ini]), ftol=big"1e-40")
       v = r.zero[1]
   else
       r = nlsolve((vout, vin)->vout .= func(vin, params...), ini, ftol=big"1e-40")
       v = r.zero
   end
   return v, r.f_converged
end;

function H(u)
   (r0, r2, r3, x3, y3) = u
   return [
       x3^2 - (r3 + 61/40)^2 + (r0 - y3 - 61/40)^2,  # eq1
       x3^2 - (r2 + r3)^2 + (-r0 + r2 + y3 + 61/20)^2,  # eq2
       r0^2/4 - (r0/2 + r2)^2 + (r0 - r2 - 61/20)^2,  # eq3
       y3^2 + (-r0/2 + x3)^2 - (r0/2 + r3)^2,  # eq4
       x3^2 + y3^2 - (r0 - r3)^2,  # eq5
   ]
end;

r1 = 305//200
iniv = [big"10.0", 2, 2, 2, 7]
res = nls(H, ini=iniv);
names = ("r0", "r2", "r3", "x3", "y3")
if res[2]
   for (name, x) in zip(names, res[1])
       @printf("%s = %g;  ", name, round(Float64(x), digits=6))
   end
   println()
else
   println("収束していない")
end;


   r0 = 9.51;  r2 = 1.86053;  r3 = 2.05932;  x3 = 3.33205;  y3 = 6.66409;  

外円の直径は 2r0 = 19.02 つまり,19寸0分2厘。

using Plots

function draw(more=false)
   pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   r1 = 305//200
   (r0, r2, r3, x3, y3) = [24.0, 5, 5, 9, 17]
   (r0, r2, r3, x3, y3) = res[1]
   r4 = r0/2
   @printf("r0 = %g;  r1 = %g;  r2 = %g;  r3 = %g;  x3 = %g;  y3 = %g\n", r0, r1, r2, r3, x3, y3)
   @printf("外円の直径 = %g\n", 2r0)
   plot()
   circle(0, 0, r0, :red)
   circle(0, r0 - r1, r1, :blue)
   circle(0, r1 - r0, r1, :blue)
   circle(0, r0 - 2r1 - r2, r2, :brown)
   circle(0, 2r1 + r2 - r0, r2, :brown)
   circle4(x3, y3, r3, :green)
   circle(r4, 0, r4, :orange)
   circle(-r4, 0, r4, :orange)
   if more
       delta = (fontheight = (ylims()[2]- ylims()[1]) / 500 * 10 * 2) /  3  # size[2] * fontsize * 2
       point(0, r0 - r1, " 甲円:r1\n r0-r1", :black, :left, :vcenter)
       point(0, r0 - 2r1 - r2, " 乙円:r2\n r0-2r1-r2", :black, :left, :vcenter)
       point(x3, y3, " 丙円:r3\n (x3,y3)", :black, :left, :vcenter)
       point(r4, 0, "丁円:r4,(r4,0)", :orange, :center, :bottom, delta=delta/2)
       point(0, r0, " r0", :red, :left, :bottom, delta=delta/2)
       hline!([0], color=:gray, lw=0.5)
       vline!([0], color=:gray, lw=0.5)
   else
      plot!(showaxis=false)
   end
end;

コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Juliaを使用してJupyter Labでリポータブルなデータ分析を行う方法

2023年09月28日 | Julia
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

StatsModelsを使用した統計モデリングと仮説検定

2023年09月27日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

MatplotlibとSeabornを使ったデータの視覚化の基本

2023年09月27日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その445)

2023年09月26日 | Julia

算額(その445)

享和3年癸亥4月(1803) 上州中里邑 黒崎九兵祀則
藤田貞資(1807):続神壁算法
http://www.wasan.jp/jinpeki/zokujinpekisanpo.pdf

外円内に甲円 1 個,乙円 2 個,菱形 1 個が入っている。外円と甲円の直径がそれぞれ 7 寸,3 寸のとき,乙円の直径を求めよ。

外円の半径と中心座標を r0, (0, 0)
甲円の半径と中心座標を r1, (r0 - r1, 0)
乙円の半径と中心座標を r2, (x2, y2)
とおき,連立方程式の解を求める。

include("julia-source.txt")

using SymPy

@syms r0::positive, r1::positive,
     r2::positive, x2::positive, y2::positive;

(r0, r1) = (7, 3) .// 2
eq1 = x2^2 + (r0 - r1 - y2)^2 - (r1 + r2)^2
eq2 = x2^2 + y2^2 - (r0 - r2)^2
eq3 = distance(0, r0 - 2r1, sqrt(r0^2 - r1^2), -r1, x2, y2) - r2^2 
res = solve([eq1, eq2, eq3], (r2, x2, y2));

解は 1 組得られるが,係数が分数の長い式になるので,evalf() した値を示しておく。

乙円の直径は 2 * 1.2 = 2寸4分である。

(res[1][1].evalf(), res[1][2].evalf(), res[1][3].evalf())

   (1.20000000000000, 2.24499443206436, 0.500000000000000)

using Plots

function draw(more=false)
   pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r0, r1) = (7, 3) .// 2
   (r2, x2, y2) = (res[1][1].evalf(), res[1][2].evalf(), res[1][3].evalf())
   @printf("r2 = %g;  x2 = %g;  y2 = %g\n", r2, x2, y2)
   @printf("乙円の直径 = %g\n", 2r2)
   plot()
   circle(0, 0, r0, :red)
   circle(0, r0 - r1, r1, :blue)
   circle(x2, y2, r2, :brown)
   b = sqrt(r0^2 - r1^2)
   plot!([0, b, 0, -b, 0], [-r0, -r1, r0 - 2r1, -r1, -r0], color=:orange, lw=0.5)
   if more
       delta = (fontheight = (ylims()[2]- ylims()[1]) / 500 * 10 * 2) /  3  # size[2] * fontsize * 2
       point(0, r0 - r1, " 甲円:r1, (0,r0-r1)", :black, :left, :vcenter)
       point(x2, y2, "乙円:r2,(x2,y2)", :brown, :center, :top, delta=-delta)
       point(0, r0 - 2r1, " r0-2r1")
       point(0, -r1, " -r1")
       point(0, r0, " r0", :red, :left, :bottom)
       point(sqrt(r0^2 - r1^2), -r1, "(√(r0^2-r1^2),-r1) ", :orange, :right, :vcenter)
       hline!([0], color=:gray, lw=0.5)
       vline!([0], color=:gray, lw=0.5)
   else
      plot!(showaxis=false)
   end
end;

コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その444)

2023年09月26日 | Julia

算額(その444)

享和3年癸亥4月(1803) 上州棟高邑 坂本丹治亮春
藤田貞資(1807):続神壁算法
http://www.wasan.jp/jinpeki/zokujinpekisanpo.pdf

長方形内に大円 1 個,中円,小円がそれぞれ 2 個入っており,互いに内接・外接しあっている。長方形の長辺が 40寸8分のとき,それぞれの円の直径を求めよ。

原点を図のようにおき,長方形の長辺を a,短辺を 2b とする。
大円の半径と中心座標を r1, (a - r1, 0)
中円の半径と中心座標を r2, (r2, b - r2)
小円の半径と中心座標を r3, (r3, r3)
とおき,連立方程式の解を求める。

include("julia-source.txt")

using SymPy

@syms a::positive, b::positive,
     r1::positive, r2::positive, r3::positive;

b = r1
eq1 = (a - r1 - r2)^2 + (b - r2)^2 - (r1 + r2)^2
eq2 = (a - r1 - r3)^2 + r3^2 - (r1 + r3)^2
eq3 = (r2 - r3)^2 + (b - r2 - r3)^2 - (r2 + r3)^2
res = solve([eq1, eq2, eq3], (r1, r2, r3))

   2-element Vector{Tuple{Sym, Sym, Sym}}:
    (a*(9 - 4*sqrt(2))/8, a/8, a*(3/2 - sqrt(2)))
    (a*(4*sqrt(2) + 9)/8, a/8, a*(sqrt(2) + 3/2))

最初のものが適解である。

a = 40寸8分 のとき各円の直径は 34寸1分あまり, 10寸2分, 7寸あまりである。

2(a*(9 - 4*sqrt(2))/8)(a => 40.8).evalf() |> println
2(a/8)(a => 40.8).evalf() |> println
2(a*(3/2 - sqrt(2)))(a => 40.8).evalf() |> println

   34.1000866551777
   10.2000000000000
   7.00017331035544

using Plots

function draw(more=false)
   pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   a = 408//10
   (r1, r2, r3) = (a*(9 - 4*sqrt(2))/8, a/8, a*(3/2 - sqrt(2)))
   b = r1
   @printf("r1 = %g;  r2 = %g;  r3 = %g\n", r1, r2, r3)
   plot([0, a, a, 0, 0], [-b, -b, b, b, -b], color=:black, lw=0.5)
   circle(a - r1, 0, r1, :red)
   circle(r2, b - r2, r2, :blue)
   circle(r2, r2 - b, r2, :blue)
   circle(r3, r3, r3, :brown)
   circle(r3, -r3, r3, :brown)
   if more
       delta = (fontheight = (ylims()[2]- ylims()[1]) / 500 * 10 * 2) /  3  # size[2] * fontsize * 2
       point(a - r1, 0, " 大円:r1,(a-r1,0)", :red, delta=-delta)
       point(r2, b - r2, " 中円:r2,(r2,b-r2)", :black)
       point(r3, r3, " 小円:r3,(r3,r3)", :black)
       point(a, 0, "a ", :black, :right, :bottom, delta=delta)
       hline!([0], color=:gray, lw=0.5)
       vline!([0], color=:gray, lw=0.5)
   else
      plot!(showaxis=false)
   end
end;

コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その443)

2023年09月26日 | Julia

算額(その443)

天明五年乙巳秋九月(1785) 藤田貞資門人 筑後州久留米藩 清水與市道香

藤田貞資(1789):神壁算法巻下
http://www.wasan.jp/jinpeki/jinpekisanpo2.pdf

求めるものと条件として与えられる数値が異なるだけで,算額(その31)と基本的に同じである。

図のように,直線の上に大円,中円,小円,甲円,乙円がそれぞれ外接しあっている。甲円,乙円の径を98寸1厘,121寸 としたとき,中円の径を求めよ。

それぞれの円の中心座標と半径を (0, r1, r1), (x2, r2, r2), (x3, r3, r3), (x4, r4, r4), (x5, r5, r5) とする。

include("julia-source.txt")

using SymPy

@syms r1, r2, r3::positive, r4::positive, r5::positive
@syms x2::positive, x3::positive, x4::positive, x5::positive;

(r4, r5) = (981, 1210) .// 20;
eq1 = x3^2 + (r1 - r3)^2 - (r1 + r3)^2;
eq2 = x2^2 + (r1 - r2)^2 - (r1 + r2)^2;
eq3 = (x2 - x3)^2 + (r2 - r3)^2 - (r2 + r3)^2;
eq4 = x5^2 + (r1 - r5)^2 - (r1 + r5)^2;
eq5 = (x3 - x4)^2 + (r3 - r4)^2 - (r3 + r4)^2;
eq6 = (x2 - x4)^2 + (r2 - r4)^2 - (r2 + r4)^2;
eq7 = (x5 - x3)^2 + (r3 - r5)^2 - (r3 + r5)^2;

res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7], (r1, r2, r3, x2, x3, x4, x5))

   2-element Vector{NTuple{7, Sym}}:
    (3917133*sqrt(1090)/3682898 + 304705467/7365796, 121/2, 3917133*sqrt(1090)/52441 + 260073891/104882, 3993*sqrt(1090)/2714 + 118701/1357, 188259786/310753 + 11751399*sqrt(1090)/621506, 118701/1357 + 32373*sqrt(1090)/6785, 3993*sqrt(1090)/2714 + 118701/1357)
    (35254197*sqrt(1090)/3682898 + 2742349203/7365796, 70508394*sqrt(1090)/14891881 + 6218626689/29783762, 2340665019/104882 - 35254197*sqrt(1090)/52441, 2340665019/5236663 + 176270985*sqrt(1090)/10473326, -401684184/310753 + 35254197*sqrt(1090)/621506, 356103/1357 + 97119*sqrt(1090)/6785, 11979*sqrt(1090)/2714 + 356103/1357)

2番めのものが適解である。

name = ["r1", "r2", "r3", "x2", "x3", "x4", "x5"]
j = 2
for i in 1:7
   println("$(name[i]) = $(res[j][i].evalf())")
end

   r1 = 688.343020749222
   r2 = 365.108908025960
   r3 = 122.232157448001
   x2 = 1002.63686078867
   x3 = 580.129821644955
   x4 = 734.990886123080
   x5 = 408.140920542540

中円の直径は 730.21781605192 である。算額の答えでは 729 となっており,かなり違いがある。

2res[2][2].evalf()

   730.21781605192

using Plots

function draw(more=false)
   pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r4, r5) = (981, 1210) ./ 20
   r1, r2, r3, x2, x3, x4, x5 = (35254197*sqrt(1090)/3682898 + 2742349203/7365796, 70508394*sqrt(1090)/14891881 + 6218626689/29783762, 2340665019/104882 - 35254197*sqrt(1090)/52441, 2340665019/5236663 + 176270985*sqrt(1090)/10473326, -401684184/310753 + 35254197*sqrt(1090)/621506, 356103/1357 + 97119*sqrt(1090)/6785, 11979*sqrt(1090)/2714 + 356103/1357)
   @printf("r1 = %g;  r2 = %g;  r3 = %g;  r4 = %g;  r5 = %g\n", r1, r2, r3, r4, r5)
   @printf("中円の直径 = %g\n", 2r2)
   plot()
   circle(0, r1, r1, :red)
   circle(x2, r2, r2, :blue)
   circle(x3, r3, r3, :brown)
   circle(x4, r4, r4, :green)
   circle(x5, r5, r5, :red)
   hline!([0], color=:black, linewidth=0.25)
   if more
       point(0, r1, "大円 r1")
       point(x2, r2,"中円 r2")
       point(x3, r3,"小")
       point(x4, r4,"乙")
       point(x5, r5,"甲")
   end
end;

コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Plotlyを使用した対話型データ可視化の作成

2023年09月26日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

NumPyとPandasを組み合わせて高速なデータ操作を実現する方法

2023年09月26日 | Python
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

対話型データ可視化の方法と実際 - Juliaを使ってデータを探索し理解する

2023年09月25日 | Julia
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Julia コードのパフォーマンスを向上させる方法

2023年09月25日 | Julia
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

アルゴリズムの効率的なJulia実装とパフォーマンスの最適化

2023年09月25日 | Julia
コメント
  • Twitterでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

PVアクセスランキング にほんブログ村

PVアクセスランキング にほんブログ村