mirror of
https://gitlab.com/MisterBiggs/julia-for-matlabbers.git
synced 2025-06-16 07:06:48 +00:00
more writing
This commit is contained in:
parent
c02c318249
commit
8bb6cb9984
228
intro/intro.jl
228
intro/intro.jl
@ -1,5 +1,5 @@
|
|||||||
### A Pluto.jl notebook ###
|
### A Pluto.jl notebook ###
|
||||||
# v0.14.7
|
# v0.14.8
|
||||||
|
|
||||||
using Markdown
|
using Markdown
|
||||||
using InteractiveUtils
|
using InteractiveUtils
|
||||||
@ -25,6 +25,12 @@ begin
|
|||||||
using LaTeXStrings
|
using LaTeXStrings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 3228a21e-cab8-4738-bdc5-a6827c764c06
|
||||||
|
using Unitful
|
||||||
|
|
||||||
|
# ╔═╡ 2df0563d-054a-4ad0-97af-6e06b31a7b1d
|
||||||
|
using LinearAlgebra
|
||||||
|
|
||||||
# ╔═╡ bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
# ╔═╡ bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
||||||
md"""
|
md"""
|
||||||
# Julia for People That Were Unfortunate Enough to Learn MATLAB
|
# Julia for People That Were Unfortunate Enough to Learn MATLAB
|
||||||
@ -265,6 +271,197 @@ f(1, 2, 3)
|
|||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 916f5f09-1aa3-47a7-9c66-c42513eaccea
|
||||||
|
md"""
|
||||||
|
## Things Julia Does Well
|
||||||
|
|
||||||
|
There are a few ways that Julia really shines that make it perfect for engineering and science. Below are a few examples.
|
||||||
|
|
||||||
|
## Symbols
|
||||||
|
|
||||||
|
Julia supports a wide variety of unicode symbols in the language that can be accessed with the syntax `\pi{tab}` to get π. This is a great way to make code closer to LaTex or handwritten examples which makes it easier to read or understand.
|
||||||
|
|
||||||
|
* `\theta{tab}` θ
|
||||||
|
* `q\bar{tab}` q̄
|
||||||
|
* `x\ddot{tab}` ẍ
|
||||||
|
|
||||||
|
## List Comprehension
|
||||||
|
|
||||||
|
List comprehensions can be thought of single line for loops and can be very handy in a variety of scenarios. The syntax is:
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Add 1 to each number in a list of numbers
|
||||||
|
[number + 1 for number in numbers]
|
||||||
|
```
|
||||||
|
|
||||||
|
and would return a new list of numbers.
|
||||||
|
|
||||||
|
List comprehensions are especially useful instead of one line for loops, or when you need to create an array to pass into a function. List comprehensions can be nested, and also support `if` in order to filter values making them extremely powerful:
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 2172de3a-7dba-4cbc-9c04-444db595c328
|
||||||
|
[x for x in 1:12 if x % 3 == 0]
|
||||||
|
|
||||||
|
# ╔═╡ fd0f5cf6-c90b-4492-9c72-9e707803e736
|
||||||
|
md"""
|
||||||
|
Since `append!` is usually very slow you can see below that using a list comprehension to create a vector is almost 10x faster.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ a2c7e472-2460-428e-bfec-acf925fad89d
|
||||||
|
begin
|
||||||
|
t = []
|
||||||
|
for i in 1:1e6
|
||||||
|
append!(t,i)
|
||||||
|
end
|
||||||
|
t
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ bce2eecc-dc01-4bf6-9eaa-880fd8a69768
|
||||||
|
[i for i in 1:1e6]
|
||||||
|
|
||||||
|
# ╔═╡ 9ea4584c-8177-4f69-9739-b2faa93281a8
|
||||||
|
md"""
|
||||||
|
---
|
||||||
|
|
||||||
|
### Matrix Math
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ a2df1c35-c0b2-45bb-86b7-57e208287392
|
||||||
|
md"""
|
||||||
|
---
|
||||||
|
|
||||||
|
### Units
|
||||||
|
|
||||||
|
Julia has an unofficial units library that is incredibly powerful and much easier to use than Matlabs units functionality. It is simply import as `Unitful`
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 7b9b11f2-c6d3-4dab-ab30-6e47a3a4bfb3
|
||||||
|
md"""
|
||||||
|
Making a value with a unit is as simple as using the `u` string macro like the following:
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 09bc71cd-7bc0-4d5a-bcf8-984abe273375
|
||||||
|
mass = 10u"lb"
|
||||||
|
|
||||||
|
# ╔═╡ 744b7e78-0e5b-468d-98b1-a5511f7db311
|
||||||
|
gravity = 9.81u"m/s^2"
|
||||||
|
|
||||||
|
# ╔═╡ ea6b28d5-f82b-4a21-9ca4-13215c6912b3
|
||||||
|
mass + gravity # The library automatically detects if units can be added to eachother.
|
||||||
|
|
||||||
|
# ╔═╡ 871664ac-5a93-4718-ae1a-c9f59f6b8374
|
||||||
|
weight = mass * gravity # You can mix unit systems
|
||||||
|
|
||||||
|
# ╔═╡ dd38ffe4-4efb-4571-b5b2-76b811901387
|
||||||
|
md"""
|
||||||
|
You'll notice that the weight was left as a gross collection of units instead of automatically converting to `N` like we woule expect. This is usually the case but can easily be fixed by using Julias pipe operator, `|>`.
|
||||||
|
|
||||||
|
!!! note "The Pipe Operator"
|
||||||
|
The pipe operator, or `|>` is a great way to chain function calls in Julia. Using `x |> f` is the exact same as using `f(x)` but in many cases can be much clearer.
|
||||||
|
|
||||||
|
```julia
|
||||||
|
inv(sum((x->x.^2 .+ 1)(1:10))) # Hard to read
|
||||||
|
1:10 |> x -> x.^2 .+ 1 |> sum |> inv # Easier to read
|
||||||
|
```
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ ade2979d-155e-4f20-82db-6da141bb8eaa
|
||||||
|
weight |> u"N"
|
||||||
|
|
||||||
|
# ╔═╡ 8d4de01a-197f-40cb-942f-bfec335e3844
|
||||||
|
md"""
|
||||||
|
Units can also be stripped to give a normal number again.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 86da848f-1e84-47c8-8a79-373f241b6025
|
||||||
|
ustrip(u"N", weight)
|
||||||
|
|
||||||
|
# ╔═╡ f43812d2-9f87-489a-9317-e7aa19ffd3bc
|
||||||
|
md"""
|
||||||
|
---
|
||||||
|
|
||||||
|
### Custom Data Types
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 44daff45-f608-47e3-ad6e-fca43532f322
|
||||||
|
struct Quaternion
|
||||||
|
i::Float64
|
||||||
|
j::Float64
|
||||||
|
k::Float64
|
||||||
|
r::Float64
|
||||||
|
|
||||||
|
Quaternion(i, j, k, r) = norm([i, j, k, r]) ≈ 1 ? new(i, j, k, r) : error("Magnitude not equal to 1.")
|
||||||
|
Quaternion(q) = Quaternion(q[1], q[2], q[3], q[4])
|
||||||
|
Quaternion() = new(0, 0, 0, 1)
|
||||||
|
|
||||||
|
Quaternion(yaw, pitch, roll) = Quaternion([0 0 sin(yaw / 2) cos(yaw / 2)]) * Quaternion([0 sin(pitch / 2) 0 cos(pitch / 2)]) * Quaternion([sin(roll / 2) 0 0 cos(roll / 2)])
|
||||||
|
|
||||||
|
function Quaternion(ē, ϕ)
|
||||||
|
if sum(ē) == 0
|
||||||
|
return Quaternion([0 0 0 cos(ϕ / 2)])
|
||||||
|
end
|
||||||
|
dir = normalize(ē) * sin(ϕ / 2)
|
||||||
|
return Quaternion(dir[1], dir[2], dir[3], cos(ϕ / 2))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# ╔═╡ 37c4a8ef-d503-497a-a63c-3e43d0ebb2a7
|
||||||
|
function QuaternionMultiplication(l::Quaternion, r::Quaternion)
|
||||||
|
R = [r.r r.k r.j r.i;
|
||||||
|
-r.k r.r r.i r.j;
|
||||||
|
r.j -r.i r.r r.k;
|
||||||
|
-r.i -r.j -r.k r.r]
|
||||||
|
L = [l.i; l.j; l.k; l.r]
|
||||||
|
return Quaternion(R * L)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 0d67e350-a87e-4a07-a01c-d348f3e62599
|
||||||
|
Base.:*(l::Quaternion, r::Quaternion) = QuaternionMultiplication(l::Quaternion, r::Quaternion)
|
||||||
|
|
||||||
|
# ╔═╡ cbf7d0fb-855b-442b-80c2-cfb6dcabe104
|
||||||
|
begin
|
||||||
|
Base.iterate(q::Quaternion, state=1) = state > 4 ? nothing : (collect(q)[state], state + 1)
|
||||||
|
Base.length(q::Quaternion) = 4
|
||||||
|
Base.collect(q::Quaternion) = [q.i q.j q.k q.r]
|
||||||
|
Base.getindex(q::Quaternion, i) = collect(q)[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 84e6fb12-be8c-4e00-bf9c-d45748347def
|
||||||
|
begin
|
||||||
|
LinearAlgebra.norm(q::Quaternion) = norm(collect(q))
|
||||||
|
LinearAlgebra.normalize(q::Quaternion) = collect(q) / norm(q)
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 2ba455ff-70f5-47d5-9fe8-bf4ebee9386a
|
||||||
|
q1 = Quaternion(1,0,0,0)
|
||||||
|
|
||||||
|
# ╔═╡ 6895bcb2-9a11-4f0a-b1c2-6640f6fb509c
|
||||||
|
q2 = Quaternion([1 2 3], π)
|
||||||
|
|
||||||
|
# ╔═╡ fafa0a8d-5ae5-49e9-8d11-4abb5748431e
|
||||||
|
q1*q2
|
||||||
|
|
||||||
|
# ╔═╡ 8924673f-e99c-44b3-be6d-2abf0b2f5e23
|
||||||
|
md"""
|
||||||
|
---
|
||||||
|
|
||||||
|
### Tabular Data
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ fbc06121-d53a-4a64-9497-63d7f3583fbb
|
||||||
|
md"""
|
||||||
|
## Things Julia Does Poorly
|
||||||
|
|
||||||
|
Performance
|
||||||
|
Libraries
|
||||||
|
Still young and breaking things
|
||||||
|
"""
|
||||||
|
|
||||||
# ╔═╡ Cell order:
|
# ╔═╡ Cell order:
|
||||||
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
||||||
# ╟─307cbf7a-1ac5-47a5-8031-3458f9dd1887
|
# ╟─307cbf7a-1ac5-47a5-8031-3458f9dd1887
|
||||||
@ -310,3 +507,32 @@ f(1, 2, 3)
|
|||||||
# ╠═55e0d022-faca-40f8-a4a9-3a501eb5d19e
|
# ╠═55e0d022-faca-40f8-a4a9-3a501eb5d19e
|
||||||
# ╟─dc89d08d-29bf-4e04-8470-c79c88ab4689
|
# ╟─dc89d08d-29bf-4e04-8470-c79c88ab4689
|
||||||
# ╟─b30e6f73-fe46-45e7-898e-dd1205dffe68
|
# ╟─b30e6f73-fe46-45e7-898e-dd1205dffe68
|
||||||
|
# ╟─916f5f09-1aa3-47a7-9c66-c42513eaccea
|
||||||
|
# ╠═2172de3a-7dba-4cbc-9c04-444db595c328
|
||||||
|
# ╟─fd0f5cf6-c90b-4492-9c72-9e707803e736
|
||||||
|
# ╠═a2c7e472-2460-428e-bfec-acf925fad89d
|
||||||
|
# ╠═bce2eecc-dc01-4bf6-9eaa-880fd8a69768
|
||||||
|
# ╠═9ea4584c-8177-4f69-9739-b2faa93281a8
|
||||||
|
# ╠═a2df1c35-c0b2-45bb-86b7-57e208287392
|
||||||
|
# ╠═3228a21e-cab8-4738-bdc5-a6827c764c06
|
||||||
|
# ╠═7b9b11f2-c6d3-4dab-ab30-6e47a3a4bfb3
|
||||||
|
# ╠═09bc71cd-7bc0-4d5a-bcf8-984abe273375
|
||||||
|
# ╠═744b7e78-0e5b-468d-98b1-a5511f7db311
|
||||||
|
# ╠═ea6b28d5-f82b-4a21-9ca4-13215c6912b3
|
||||||
|
# ╠═871664ac-5a93-4718-ae1a-c9f59f6b8374
|
||||||
|
# ╟─dd38ffe4-4efb-4571-b5b2-76b811901387
|
||||||
|
# ╠═ade2979d-155e-4f20-82db-6da141bb8eaa
|
||||||
|
# ╟─8d4de01a-197f-40cb-942f-bfec335e3844
|
||||||
|
# ╠═86da848f-1e84-47c8-8a79-373f241b6025
|
||||||
|
# ╟─f43812d2-9f87-489a-9317-e7aa19ffd3bc
|
||||||
|
# ╠═2df0563d-054a-4ad0-97af-6e06b31a7b1d
|
||||||
|
# ╠═44daff45-f608-47e3-ad6e-fca43532f322
|
||||||
|
# ╠═37c4a8ef-d503-497a-a63c-3e43d0ebb2a7
|
||||||
|
# ╠═0d67e350-a87e-4a07-a01c-d348f3e62599
|
||||||
|
# ╠═cbf7d0fb-855b-442b-80c2-cfb6dcabe104
|
||||||
|
# ╠═84e6fb12-be8c-4e00-bf9c-d45748347def
|
||||||
|
# ╠═2ba455ff-70f5-47d5-9fe8-bf4ebee9386a
|
||||||
|
# ╠═6895bcb2-9a11-4f0a-b1c2-6640f6fb509c
|
||||||
|
# ╠═fafa0a8d-5ae5-49e9-8d11-4abb5748431e
|
||||||
|
# ╠═8924673f-e99c-44b3-be6d-2abf0b2f5e23
|
||||||
|
# ╠═fbc06121-d53a-4a64-9497-63d7f3583fbb
|
||||||
|
Loading…
x
Reference in New Issue
Block a user