Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active January 20, 2021 07:35
Show Gist options
  • Save genkuroki/ef0c4bfd0dbd00b810b93f365ca2097a to your computer and use it in GitHub Desktop.
Save genkuroki/ef0c4bfd0dbd00b810b93f365ca2097a to your computer and use it in GitHub Desktop.
plot Dict and Vector{Any}
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# plot Dict and Vector{Any}\n\nhttps://twitter.com/genkuroki/status/1351783595933798401"
},
{
"metadata": {
"toc": true
},
"cell_type": "markdown",
"source": "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#plot-Dict\" data-toc-modified-id=\"plot-Dict-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>plot Dict</a></span></li><li><span><a href=\"#Vector{Complex}問題\" data-toc-modified-id=\"Vector{Complex}問題-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Vector{Complex}問題</a></span></li><li><span><a href=\"#Vector{Any}問題\" data-toc-modified-id=\"Vector{Any}問題-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Vector{Any}問題</a></span></li></ul></div>"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## plot Dict"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "d = Dict(:foo => 123, :bar => 456, :baz => 789)",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "Dict{Symbol, Int64} with 3 entries:\n :baz => 789\n :bar => 456\n :foo => 123"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = []\nfor k in keys(d)\n append!(a, d[k])\nend\na",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "3-element Vector{Any}:\n 789\n 456\n 123"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "keys(d)",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "KeySet for a Dict{Symbol, Int64} with 3 entries. Keys:\n :baz\n :bar\n :foo"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "values(d)",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "ValueIterator for a Dict{Symbol, Int64} with 3 entries. Values:\n 789\n 456\n 123"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "length(d)",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": false
},
"cell_type": "code",
"source": "using Plots\nbar(collect(values(d)); xtick=(1:length(d), keys(d)), label=\"\")",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n<defs>\n <clipPath id=\"clip430\">\n <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip430)\" d=\"\nM0 1600 L2400 1600 L2400 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip431\">\n <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip430)\" d=\"\nM166.205 1486.45 L2352.76 1486.45 L2352.76 47.2441 L166.205 47.2441 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip432\">\n <rect x=\"166\" y=\"47\" width=\"2188\" height=\"1440\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 564.472,1486.45 564.472,47.2441 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1259.48,1486.45 1259.48,47.2441 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1954.49,1486.45 1954.49,47.2441 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,1486.45 2352.76,1486.45 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 564.472,1486.45 564.472,1469.18 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1259.48,1486.45 1259.48,1469.18 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1954.49,1486.45 1954.49,1469.18 \n \"/>\n<path clip-path=\"url(#clip430)\" d=\"M 0 0 M547.852 1534.18 Q547.852 1529.48 545.907 1526.82 Q543.986 1524.13 540.607 1524.13 Q537.227 1524.13 535.282 1526.82 Q533.361 1529.48 533.361 1534.18 Q533.361 1538.88 535.282 1541.56 Q537.227 1544.23 540.607 1544.23 Q543.986 1544.23 545.907 1541.56 Q547.852 1538.88 547.852 1534.18 M533.361 1525.13 Q534.704 1522.81 536.741 1521.7 Q538.801 1520.57 541.648 1520.57 Q546.37 1520.57 549.31 1524.32 Q552.273 1528.07 552.273 1534.18 Q552.273 1540.29 549.31 1544.04 Q546.37 1547.79 541.648 1547.79 Q538.801 1547.79 536.741 1546.68 Q534.704 1545.55 533.361 1543.23 L533.361 1547.12 L529.079 1547.12 L529.079 1511.1 L533.361 1511.1 L533.361 1525.13 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M568.523 1534.09 Q563.361 1534.09 561.37 1535.27 Q559.38 1536.45 559.38 1539.3 Q559.38 1541.56 560.861 1542.91 Q562.366 1544.23 564.935 1544.23 Q568.477 1544.23 570.606 1541.73 Q572.759 1539.2 572.759 1535.04 L572.759 1534.09 L568.523 1534.09 M577.018 1532.33 L577.018 1547.12 L572.759 1547.12 L572.759 1543.18 Q571.301 1545.55 569.125 1546.68 Q566.949 1547.79 563.801 1547.79 Q559.819 1547.79 557.458 1545.57 Q555.12 1543.32 555.12 1539.57 Q555.12 1535.2 558.037 1532.98 Q560.977 1530.75 566.787 1530.75 L572.759 1530.75 L572.759 1530.34 Q572.759 1527.4 570.815 1525.8 Q568.893 1524.18 565.398 1524.18 Q563.176 1524.18 561.069 1524.71 Q558.963 1525.24 557.018 1526.31 L557.018 1522.37 Q559.356 1521.47 561.555 1521.03 Q563.755 1520.57 565.838 1520.57 Q571.463 1520.57 574.241 1523.49 Q577.018 1526.4 577.018 1532.33 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M579.634 1521.19 L599.865 1521.19 L599.865 1525.08 L583.847 1543.72 L599.865 1543.72 L599.865 1547.12 L579.055 1547.12 L579.055 1543.23 L595.074 1524.6 L579.634 1524.6 L579.634 1521.19 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1244.54 1534.18 Q1244.54 1529.48 1242.59 1526.82 Q1240.67 1524.13 1237.29 1524.13 Q1233.91 1524.13 1231.97 1526.82 Q1230.05 1529.48 1230.05 1534.18 Q1230.05 1538.88 1231.97 1541.56 Q1233.91 1544.23 1237.29 1544.23 Q1240.67 1544.23 1242.59 1541.56 Q1244.54 1538.88 1244.54 1534.18 M1230.05 1525.13 Q1231.39 1522.81 1233.43 1521.7 Q1235.49 1520.57 1238.33 1520.57 Q1243.06 1520.57 1246 1524.32 Q1248.96 1528.07 1248.96 1534.18 Q1248.96 1540.29 1246 1544.04 Q1243.06 1547.79 1238.33 1547.79 Q1235.49 1547.79 1233.43 1546.68 Q1231.39 1545.55 1230.05 1543.23 L1230.05 1547.12 L1225.77 1547.12 L1225.77 1511.1 L1230.05 1511.1 L1230.05 1525.13 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1265.21 1534.09 Q1260.05 1534.09 1258.06 1535.27 Q1256.07 1536.45 1256.07 1539.3 Q1256.07 1541.56 1257.55 1542.91 Q1259.05 1544.23 1261.62 1544.23 Q1265.16 1544.23 1267.29 1541.73 Q1269.45 1539.2 1269.45 1535.04 L1269.45 1534.09 L1265.21 1534.09 M1273.7 1532.33 L1273.7 1547.12 L1269.45 1547.12 L1269.45 1543.18 Q1267.99 1545.55 1265.81 1546.68 Q1263.64 1547.79 1260.49 1547.79 Q1256.51 1547.79 1254.14 1545.57 Q1251.81 1543.32 1251.81 1539.57 Q1251.81 1535.2 1254.72 1532.98 Q1257.66 1530.75 1263.47 1530.75 L1269.45 1530.75 L1269.45 1530.34 Q1269.45 1527.4 1267.5 1525.8 Q1265.58 1524.18 1262.08 1524.18 Q1259.86 1524.18 1257.76 1524.71 Q1255.65 1525.24 1253.7 1526.31 L1253.7 1522.37 Q1256.04 1521.47 1258.24 1521.03 Q1260.44 1520.57 1262.52 1520.57 Q1268.15 1520.57 1270.93 1523.49 Q1273.7 1526.4 1273.7 1532.33 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1293.2 1525.18 Q1292.48 1524.76 1291.62 1524.57 Q1290.79 1524.37 1289.77 1524.37 Q1286.16 1524.37 1284.21 1526.73 Q1282.29 1529.06 1282.29 1533.46 L1282.29 1547.12 L1278.01 1547.12 L1278.01 1521.19 L1282.29 1521.19 L1282.29 1525.22 Q1283.64 1522.86 1285.79 1521.73 Q1287.94 1520.57 1291.02 1520.57 Q1291.46 1520.57 1291.99 1520.64 Q1292.52 1520.68 1293.17 1520.8 L1293.2 1525.18 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1936.33 1511.1 L1936.33 1514.64 L1932.25 1514.64 Q1929.96 1514.64 1929.06 1515.57 Q1928.18 1516.5 1928.18 1518.9 L1928.18 1521.19 L1935.19 1521.19 L1935.19 1524.5 L1928.18 1524.5 L1928.18 1547.12 L1923.9 1547.12 L1923.9 1524.5 L1919.82 1524.5 L1919.82 1521.19 L1923.9 1521.19 L1923.9 1519.39 Q1923.9 1515.06 1925.91 1513.09 Q1927.93 1511.1 1932.3 1511.1 L1936.33 1511.1 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1950.84 1524.18 Q1947.42 1524.18 1945.43 1526.87 Q1943.44 1529.53 1943.44 1534.18 Q1943.44 1538.83 1945.4 1541.52 Q1947.39 1544.18 1950.84 1544.18 Q1954.25 1544.18 1956.24 1541.49 Q1958.23 1538.81 1958.23 1534.18 Q1958.23 1529.57 1956.24 1526.89 Q1954.25 1524.18 1950.84 1524.18 M1950.84 1520.57 Q1956.4 1520.57 1959.57 1524.18 Q1962.74 1527.79 1962.74 1534.18 Q1962.74 1540.55 1959.57 1544.18 Q1956.4 1547.79 1950.84 1547.79 Q1945.26 1547.79 1942.09 1544.18 Q1938.94 1540.55 1938.94 1534.18 Q1938.94 1527.79 1942.09 1524.18 Q1945.26 1520.57 1950.84 1520.57 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M1977.25 1524.18 Q1973.83 1524.18 1971.84 1526.87 Q1969.85 1529.53 1969.85 1534.18 Q1969.85 1538.83 1971.81 1541.52 Q1973.81 1544.18 1977.25 1544.18 Q1980.66 1544.18 1982.65 1541.49 Q1984.64 1538.81 1984.64 1534.18 Q1984.64 1529.57 1982.65 1526.89 Q1980.66 1524.18 1977.25 1524.18 M1977.25 1520.57 Q1982.81 1520.57 1985.98 1524.18 Q1989.15 1527.79 1989.15 1534.18 Q1989.15 1540.55 1985.98 1544.18 Q1982.81 1547.79 1977.25 1547.79 Q1971.68 1547.79 1968.5 1544.18 Q1965.36 1540.55 1965.36 1534.18 Q1965.36 1527.79 1968.5 1524.18 Q1971.68 1520.57 1977.25 1520.57 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 166.205,1445.72 2352.76,1445.72 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 166.205,1101.55 2352.76,1101.55 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 166.205,757.381 2352.76,757.381 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 166.205,413.214 2352.76,413.214 \n \"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 166.205,69.0471 2352.76,69.0471 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,1486.45 166.205,47.2441 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,1445.72 192.443,1445.72 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,1101.55 192.443,1101.55 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,757.381 192.443,757.381 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,413.214 192.443,413.214 \n \"/>\n<polyline clip-path=\"url(#clip430)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 166.205,69.0471 192.443,69.0471 \n \"/>\n<path clip-path=\"url(#clip430)\" d=\"M 0 0 M118.26 1431.51 Q114.649 1431.51 112.82 1435.08 Q111.015 1438.62 111.015 1445.75 Q111.015 1452.86 112.82 1456.42 Q114.649 1459.96 118.26 1459.96 Q121.895 1459.96 123.7 1456.42 Q125.529 1452.86 125.529 1445.75 Q125.529 1438.62 123.7 1435.08 Q121.895 1431.51 118.26 1431.51 M118.26 1427.81 Q124.07 1427.81 127.126 1432.42 Q130.205 1437 130.205 1445.75 Q130.205 1454.48 127.126 1459.08 Q124.07 1463.67 118.26 1463.67 Q112.45 1463.67 109.371 1459.08 Q106.316 1454.48 106.316 1445.75 Q106.316 1437 109.371 1432.42 Q112.45 1427.81 118.26 1427.81 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M59.8578 1114.89 L76.1772 1114.89 L76.1772 1118.83 L54.2328 1118.83 L54.2328 1114.89 Q56.8949 1112.14 61.4782 1107.51 Q66.0846 1102.86 67.2652 1101.51 Q69.5105 1098.99 70.3902 1097.25 Q71.2929 1095.5 71.2929 1093.81 Q71.2929 1091.05 69.3485 1089.31 Q67.4272 1087.58 64.3254 1087.58 Q62.1263 1087.58 59.6726 1088.34 Q57.2421 1089.11 54.4643 1090.66 L54.4643 1085.94 Q57.2884 1084.8 59.7421 1084.22 Q62.1958 1083.64 64.2328 1083.64 Q69.6031 1083.64 72.7976 1086.33 Q75.992 1089.01 75.992 1093.5 Q75.992 1095.63 75.1818 1097.56 Q74.3948 1099.45 72.2883 1102.05 Q71.7096 1102.72 68.6078 1105.94 Q65.5059 1109.13 59.8578 1114.89 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M91.2465 1087.35 Q87.6354 1087.35 85.8068 1090.91 Q84.0012 1094.45 84.0012 1101.58 Q84.0012 1108.69 85.8068 1112.25 Q87.6354 1115.8 91.2465 1115.8 Q94.8808 1115.8 96.6863 1112.25 Q98.515 1108.69 98.515 1101.58 Q98.515 1094.45 96.6863 1090.91 Q94.8808 1087.35 91.2465 1087.35 M91.2465 1083.64 Q97.0567 1083.64 100.112 1088.25 Q103.191 1092.83 103.191 1101.58 Q103.191 1110.31 100.112 1114.92 Q97.0567 1119.5 91.2465 1119.5 Q85.4364 1119.5 82.3577 1114.92 Q79.3022 1110.31 79.3022 1101.58 Q79.3022 1092.83 82.3577 1088.25 Q85.4364 1083.64 91.2465 1083.64 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M118.26 1087.35 Q114.649 1087.35 112.82 1090.91 Q111.015 1094.45 111.015 1101.58 Q111.015 1108.69 112.82 1112.25 Q114.649 1115.8 118.26 1115.8 Q121.895 1115.8 123.7 1112.25 Q125.529 1108.69 125.529 1101.58 Q125.529 1094.45 123.7 1090.91 Q121.895 1087.35 118.26 1087.35 M118.26 1083.64 Q124.07 1083.64 127.126 1088.25 Q130.205 1092.83 130.205 1101.58 Q130.205 1110.31 127.126 1114.92 Q124.07 1119.5 118.26 1119.5 Q112.45 1119.5 109.371 1114.92 Q106.316 1110.31 106.316 1101.58 Q106.316 1092.83 109.371 1088.25 Q112.45 1083.64 118.26 1083.64 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M66.5939 744.175 L54.7884 762.624 L66.5939 762.624 L66.5939 744.175 M65.367 740.101 L71.2466 740.101 L71.2466 762.624 L76.1772 762.624 L76.1772 766.513 L71.2466 766.513 L71.2466 774.661 L66.5939 774.661 L66.5939 766.513 L50.9921 766.513 L50.9921 761.999 L65.367 740.101 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M91.2465 743.18 Q87.6354 743.18 85.8068 746.745 Q84.0012 750.287 84.0012 757.416 Q84.0012 764.523 85.8068 768.087 Q87.6354 771.629 91.2465 771.629 Q94.8808 771.629 96.6863 768.087 Q98.515 764.523 98.515 757.416 Q98.515 750.287 96.6863 746.745 Q94.8808 743.18 91.2465 743.18 M91.2465 739.476 Q97.0567 739.476 100.112 744.083 Q103.191 748.666 103.191 757.416 Q103.191 766.143 100.112 770.749 Q97.0567 775.333 91.2465 775.333 Q85.4364 775.333 82.3577 770.749 Q79.3022 766.143 79.3022 757.416 Q79.3022 748.666 82.3577 744.083 Q85.4364 739.476 91.2465 739.476 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M118.26 743.18 Q114.649 743.18 112.82 746.745 Q111.015 750.287 111.015 757.416 Q111.015 764.523 112.82 768.087 Q114.649 771.629 118.26 771.629 Q121.895 771.629 123.7 768.087 Q125.529 764.523 125.529 757.416 Q125.529 750.287 123.7 746.745 Q121.895 743.18 118.26 743.18 M118.26 739.476 Q124.07 739.476 127.126 744.083 Q130.205 748.666 130.205 757.416 Q130.205 766.143 127.126 770.749 Q124.07 775.333 118.26 775.333 Q112.45 775.333 109.371 770.749 Q106.316 766.143 106.316 757.416 Q106.316 748.666 109.371 744.083 Q112.45 739.476 118.26 739.476 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M64.6495 411.351 Q61.5013 411.351 59.6495 413.504 Q57.8208 415.656 57.8208 419.406 Q57.8208 423.133 59.6495 425.309 Q61.5013 427.462 64.6495 427.462 Q67.7976 427.462 69.6263 425.309 Q71.4781 423.133 71.4781 419.406 Q71.4781 415.656 69.6263 413.504 Q67.7976 411.351 64.6495 411.351 M73.9318 396.698 L73.9318 400.957 Q72.1726 400.124 70.367 399.684 Q68.5846 399.244 66.8254 399.244 Q62.1958 399.244 59.7421 402.369 Q57.3115 405.494 56.9643 411.814 Q58.33 409.8 60.3902 408.735 Q62.4504 407.647 64.9272 407.647 Q70.1355 407.647 73.1448 410.818 Q76.1772 413.967 76.1772 419.406 Q76.1772 424.73 73.029 427.948 Q69.8809 431.166 64.6495 431.166 Q58.6541 431.166 55.4828 426.582 Q52.3116 421.976 52.3116 413.249 Q52.3116 405.055 56.2004 400.193 Q60.0893 395.309 66.6402 395.309 Q68.3994 395.309 70.1818 395.656 Q71.9874 396.004 73.9318 396.698 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M91.2465 399.013 Q87.6354 399.013 85.8068 402.578 Q84.0012 406.119 84.0012 413.249 Q84.0012 420.355 85.8068 423.92 Q87.6354 427.462 91.2465 427.462 Q94.8808 427.462 96.6863 423.92 Q98.515 420.355 98.515 413.249 Q98.515 406.119 96.6863 402.578 Q94.8808 399.013 91.2465 399.013 M91.2465 395.309 Q97.0567 395.309 100.112 399.916 Q103.191 404.499 103.191 413.249 Q103.191 421.976 100.112 426.582 Q97.0567 431.166 91.2465 431.166 Q85.4364 431.166 82.3577 426.582 Q79.3022 421.976 79.3022 413.249 Q79.3022 404.499 82.3577 399.916 Q85.4364 395.309 91.2465 395.309 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M118.26 399.013 Q114.649 399.013 112.82 402.578 Q111.015 406.119 111.015 413.249 Q111.015 420.355 112.82 423.92 Q114.649 427.462 118.26 427.462 Q121.895 427.462 123.7 423.92 Q125.529 420.355 125.529 413.249 Q125.529 406.119 123.7 402.578 Q121.895 399.013 118.26 399.013 M118.26 395.309 Q124.07 395.309 127.126 399.916 Q130.205 404.499 130.205 413.249 Q130.205 421.976 127.126 426.582 Q124.07 431.166 118.26 431.166 Q112.45 431.166 109.371 426.582 Q106.316 421.976 106.316 413.249 Q106.316 404.499 109.371 399.916 Q112.45 395.309 118.26 395.309 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M64.3254 69.9151 Q60.9921 69.9151 59.0708 71.6975 Q57.1726 73.4799 57.1726 76.6049 Q57.1726 79.7299 59.0708 81.5123 Q60.9921 83.2947 64.3254 83.2947 Q67.6587 83.2947 69.58 81.5123 Q71.5013 79.7067 71.5013 76.6049 Q71.5013 73.4799 69.58 71.6975 Q67.6819 69.9151 64.3254 69.9151 M59.6495 67.9244 Q56.6402 67.1837 54.9504 65.1235 Q53.2838 63.0633 53.2838 60.1004 Q53.2838 55.9569 56.2236 53.5495 Q59.1865 51.1421 64.3254 51.1421 Q69.4874 51.1421 72.4272 53.5495 Q75.367 55.9569 75.367 60.1004 Q75.367 63.0633 73.6772 65.1235 Q72.0105 67.1837 69.0244 67.9244 Q72.404 68.7114 74.279 71.0031 Q76.1772 73.2947 76.1772 76.6049 Q76.1772 81.628 73.0985 84.3132 Q70.0429 86.9984 64.3254 86.9984 Q58.6078 86.9984 55.5291 84.3132 Q52.4736 81.628 52.4736 76.6049 Q52.4736 73.2947 54.3717 71.0031 Q56.2699 68.7114 59.6495 67.9244 M57.9365 60.5402 Q57.9365 63.2254 59.6032 64.73 Q61.293 66.2346 64.3254 66.2346 Q67.3346 66.2346 69.0244 64.73 Q70.7374 63.2254 70.7374 60.5402 Q70.7374 57.855 69.0244 56.3504 Q67.3346 54.8458 64.3254 54.8458 Q61.293 54.8458 59.6032 56.3504 Q57.9365 57.855 57.9365 60.5402 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M91.2465 54.8458 Q87.6354 54.8458 85.8068 58.4106 Q84.0012 61.9522 84.0012 69.0818 Q84.0012 76.1882 85.8068 79.753 Q87.6354 83.2947 91.2465 83.2947 Q94.8808 83.2947 96.6863 79.753 Q98.515 76.1882 98.515 69.0818 Q98.515 61.9522 96.6863 58.4106 Q94.8808 54.8458 91.2465 54.8458 M91.2465 51.1421 Q97.0567 51.1421 100.112 55.7485 Q103.191 60.3319 103.191 69.0818 Q103.191 77.8086 100.112 82.4151 Q97.0567 86.9984 91.2465 86.9984 Q85.4364 86.9984 82.3577 82.4151 Q79.3022 77.8086 79.3022 69.0818 Q79.3022 60.3319 82.3577 55.7485 Q85.4364 51.1421 91.2465 51.1421 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip430)\" d=\"M 0 0 M118.26 54.8458 Q114.649 54.8458 112.82 58.4106 Q111.015 61.9522 111.015 69.0818 Q111.015 76.1882 112.82 79.753 Q114.649 83.2947 118.26 83.2947 Q121.895 83.2947 123.7 79.753 Q125.529 76.1882 125.529 69.0818 Q125.529 61.9522 123.7 58.4106 Q121.895 54.8458 118.26 54.8458 M118.26 51.1421 Q124.07 51.1421 127.126 55.7485 Q130.205 60.3319 130.205 69.0818 Q130.205 77.8086 127.126 82.4151 Q124.07 86.9984 118.26 86.9984 Q112.45 86.9984 109.371 82.4151 Q106.316 77.8086 106.316 69.0818 Q106.316 60.3319 109.371 55.7485 Q112.45 51.1421 118.26 51.1421 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip432)\" d=\"\nM286.469 87.9763 L286.469 1445.72 L842.475 1445.72 L842.475 87.9763 L286.469 87.9763 L286.469 87.9763 Z\n \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 286.469,87.9763 286.469,1445.72 842.475,1445.72 842.475,87.9763 286.469,87.9763 \n \"/>\n<path clip-path=\"url(#clip432)\" d=\"\nM981.477 661.015 L981.477 1445.72 L1537.48 1445.72 L1537.48 661.015 L981.477 661.015 L981.477 661.015 Z\n \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 981.477,661.015 981.477,1445.72 1537.48,1445.72 1537.48,661.015 981.477,661.015 \n \"/>\n<path clip-path=\"url(#clip432)\" d=\"\nM1676.49 1234.05 L1676.49 1445.72 L2232.49 1445.72 L2232.49 1234.05 L1676.49 1234.05 L1676.49 1234.05 Z\n \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<polyline clip-path=\"url(#clip432)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1676.49,1234.05 1676.49,1445.72 2232.49,1445.72 2232.49,1234.05 1676.49,1234.05 \n \"/>\n</svg>\n"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Vector{Complex}問題"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools\n\nn = 10^7\na = 1/n\nb = randn(ComplexF64, n);",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct Foo\n a::Float64\n b::Vector{Complex}\nend\n\nF(x::Foo) = x.a*sum(abs2, x.b)\n\nx = Foo(a, b)\n@show typeof(x), fieldtypes(typeof(x))\nF(x)\n@btime F($x)",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "(typeof(x), fieldtypes(typeof(x))) = (Foo, (Float64, Vector{Complex}))\n 658.446 ms (20000001 allocations: 305.18 MiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "1.0004496703624222"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct Bar\n a::Float64\n b::Vector{ComplexF64}\nend\n\nF(x::Bar) = x.a*sum(abs2, x.b)\n\nx = Bar(a, b)\n@show typeof(x), fieldtypes(typeof(x))\nF(x)\n@btime F($x)",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "(typeof(x), fieldtypes(typeof(x))) = (Bar, (Float64, Vector{ComplexF64}))\n 10.477 ms (0 allocations: 0 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "1.0004496703624226"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct Baz{A, B}\n a::A\n b::B\nend\n\nF(x::Baz) = x.a*sum(abs2, x.b)\n\nx = Baz(a, b)\n@show typeof(x)\nF(x)\n@btime F($x)",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "typeof(x) = Baz{Float64, Vector{ComplexF64}}\n 10.398 ms (0 allocations: 0 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "1.0004496703624226"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Vector{Any}問題"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(a::Vector{Any}) = println(a.^2)",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "f (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f([1, 2, 3])",
"execution_count": 12,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "MethodError: no method matching f(::Vector{Int64})\nClosest candidates are:\n f(!Matched::Vector{Any}) at In[11]:1",
"traceback": [
"MethodError: no method matching f(::Vector{Int64})\nClosest candidates are:\n f(!Matched::Vector{Any}) at In[11]:1",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[12]:1",
" [2] eval",
" @ .\\boot.jl:360 [inlined]",
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base .\\loading.jl:1090"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Vector{Int} <: Vector{Any}",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "false"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Vector{Int} <: Vector{<:Any}",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(a::Vector{<:Any}) = println(a.^2)",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "g (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g([1, 2, 3])",
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Vector{<:Any}",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "Vector{var\"#s34\"} where var\"#s34\" (alias for Array{var\"#s34\", 1} where var\"#s34\")"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Vector",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "Vector{T} where T (alias for Array{T, 1} where T)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Vector{<:Any} == Vector",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "h(a::Vector) = println(a.^2)",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "h (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "h([1, 2, 3])",
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "h(1:3)",
"execution_count": 22,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "MethodError: no method matching h(::UnitRange{Int64})\nClosest candidates are:\n h(!Matched::Vector{T} where T) at In[20]:1",
"traceback": [
"MethodError: no method matching h(::UnitRange{Int64})\nClosest candidates are:\n h(!Matched::Vector{T} where T) at In[20]:1",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[22]:1",
" [2] eval",
" @ .\\boot.jl:360 [inlined]",
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base .\\loading.jl:1090"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "k(a::AbstractVector) = println(a.^2)",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "k (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "k([1, 2, 3])",
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "k(1:3)",
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "k(i for i in 1:3)",
"execution_count": 26,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "MethodError: no method matching k(::Base.Generator{UnitRange{Int64}, typeof(identity)})\nClosest candidates are:\n k(!Matched::AbstractVector{T} where T) at In[23]:1",
"traceback": [
"MethodError: no method matching k(::Base.Generator{UnitRange{Int64}, typeof(identity)})\nClosest candidates are:\n k(!Matched::AbstractVector{T} where T) at In[23]:1",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[26]:1",
" [2] eval",
" @ .\\boot.jl:360 [inlined]",
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base .\\loading.jl:1090"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "l(a) = println(a.^2)",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "l (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "l([1, 2, 3])",
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "l(1:3)",
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "l(i for i in 1:3)",
"execution_count": 30,
"outputs": [
{
"output_type": "stream",
"text": "[1, 4, 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"kernelspec": {
"name": "julia-1.6",
"display_name": "Julia 1.6.0-beta1",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.0"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "ef0c4bfd0dbd00b810b93f365ca2097a",
"data": {
"description": "plot Dict and Vector{Any}",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/ef0c4bfd0dbd00b810b93f365ca2097a"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment