Last active
December 23, 2015 06:55
-
-
Save tenfu2tea/6ffca060e098536b338e to your computer and use it in GitHub Desktop.
Juliaで、単位付き数値の算術 - Physical.jl パッケージ ref: http://qiita.com/tenfu2tea/items/3f9f6ff38e6f19bbc63a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pkg.clone("https://github.com/ggggggggg/Physical.jl.git"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> using Physical |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> (1.0 * KiloGram)* (1.0*Meter/Second)^2 | |
1000.0 g s⁻²m² | |
julia> as( ans, Joule ) | |
1.0 J | |
julia> (1.0*Volt)*(1.0*Coulomb) | |
1.0 V C | |
julia> as( ans, Joule ) | |
1.0 J |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> Physical.loaded_files | |
4-element Array{ByteString,1}: | |
"a_prefix.jl" | |
"a_si_plus.jl" | |
"b_english.jl" | |
"c_physics.jl" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Julia> 1.0 * ElectronVolt | |
1.0 eV | |
Julia> as( ans, Joule) | |
1.602176565e-19 J |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> const Shaku = DerivedUnit( "尺", 10 // 33 * Meter) | |
1 尺 | |
julia> const KenJp = DerivedUnit( "閒", 6. * Shaku) | |
1 閒 | |
julia> const Tsubo = DerivedUnit( "坪", KenJp ^2 ) | |
1 坪 | |
julia> as( 30.* Tsubo, Meter^2 ) | |
99.17355371900828 m² |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> const Liter = DerivedUnit( "L", (Deci*Meter)^3 ) | |
1 L | |
julia> (10.*(Centi*Meter))^3 | |
1000.0 cm³ | |
julia> as( ans, Liter) | |
1.0 L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> const Atmosphere = DerivedUnit("atm", 1013.25 (Hecto*Pascal) ) | |
julia> 1.0 * Atomsphere | |
1.0 atm | |
julia> as( ans, Pascal) | |
101325.0 Pa | |
julia> as(ans, Mega*Pascal) | |
0.101325 MPa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> const Inch = DerivedUnit("in", 254//10000*Meter) | |
1 in | |
julia> const Pound = DerivedUnit("lb", 453.59237*Gram) | |
1 lb | |
julia> as(0.25*Pound, Gram) | |
113.3980925 g | |
julia> const Psi = DerivedUnit("psi", Pound * g_earth_gravity / Inch^2) | |
1 psi | |
julia> as( 1.0*Psi, Pascal ) | |
6894.757293168362 Pa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> 1.0 * Atomsphere | |
1.0 atm | |
julia> as( ans, Psi) | |
14.695948775513449 psi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> [ 0., 1., 2. ] * Meter | |
[0.0,1.0,2.0] m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> ts = linspace(0,2) * Second | |
linspace(0.0,2.0,50) s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> 1.0 * Meter | |
1.0 m | |
julia> 1000.0 * (Milli*Meter) | |
1000.0 mm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> v0=13.* Meter/Second | |
13.0 s⁻¹m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> vs = v0 - g_earth_gravity .* ts | |
linspace(13.0,-6.613299999999999,50) s⁻¹m | |
julia> ys = v0 .* ts - g_earth_gravity * ts .* ts / 2. | |
[0.0,0.522443,1.02855,1.51832,1.99175,2.44884,2.8896,3.31401,3.72209,4.11384 …… | |
8.1544,8.02334,7.87594,7.71221,7.53213,7.33572,7.12297,6.89389,6.64846,6.3867] | |
m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using PyPlot | |
plot( ts.value ,vs.value, "b.") | |
ax1 = gca() | |
ax1[:set_xlabel]("time (s)") | |
ax1[:set_ylabel]("velocity (m/s)", color="b") | |
for t1 in ax1[:get_yticklabels]() | |
t1[:set_color]("b") | |
end | |
ax2 = ax1[:twinx]() | |
ax2[:plot](ts.value, ys.value, "r-") | |
ax2[:set_ylabel]("height (m)", color="r") | |
for tl in ax2[:get_yticklabels]() | |
tl[:set_color]("r") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> 1.0 * Meter == 1000.0 * (Milli*Meter) | |
true | |
julia> 0.9 * Meter == 1000.0 * (Milli*Meter) | |
false | |
julia> (1.0 + 1e-8) * Meter == 1000.0 * (Milli*Meter) | |
false | |
julia> isapprox( (1.0 + 1e-8) * Meter , 1000.0 * (Milli*Meter) ) | |
true | |
julia> 1.0 * Second == 1000.0 * (Milli*Meter) | |
ERROR: AssertionError: | |
in == at C:\Users\ ___ \.julia\v0.4\Physical\src\Quantities.jl:122 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> (1.0 * Meter).value | |
1.0 | |
julia> (1.0 * Meter).unit | |
m | |
julia> typeof(ans) | |
PUnits.Unit | |
julia> (1000.0 * (Milli*Meter)).value | |
1000.0 | |
julia> (1000.0 * (Milli*Meter)).unit | |
mm | |
julia> (1.0 * Meter).unit == (1000.0 * (Milli*Meter)).unit | |
false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> 1000.0*(Milli*Meter) + 0.1*Meter | |
1100.0 mm | |
julia> 1000.0*(Milli*Meter) + 0.1*Gram | |
ERROR: incompatible base units kg and m | |
in error at error.jl:21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> Kilogram | |
1 kg | |
julia> Gram | |
1 g | |
julia> Meter | |
1 m | |
julia> Second | |
1 s | |
julia> Ampere | |
1 A | |
julia> Kelvin | |
1 K | |
julia> Mole | |
1 mol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> 1.0 * Meter / Second | |
1.0 s⁻¹m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> (1.0*Meter) / (1.0*Second) | |
1.0 s⁻¹m | |
julia> v=(1.0*Meter)^3 | |
1.0 m³ | |
julia> v^(1/3) | |
1.0 m | |
julia> v^(2/3) | |
1.0 m² | |
julia> (1.0*Meter)/(1.0*Meter) | |
1.0 | |
julia> typeof(ans) | |
Float64 | |
julia> Complex(1.0,1.0)*Meter / (1.0*Meter) | |
1.0 + 1.0im | |
julia> typeof(ans) | |
Complex{Float64} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> as( 1000.0 * (Milli*Meter), Meter) | |
1.0 m | |
julia> as( 1000.0 * (Milli*Meter), Second) | |
ERROR: incompatible base units m and s | |
in error at error.jl:21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment