First commit
This commit is contained in:
commit
599e40cb18
|
|
@ -0,0 +1,10 @@
|
|||
Copyright (c) 2020, Enrico Lumetti
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the <organization> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
name = "Aristarkos"
|
||||
uuid = "df919aa3-a829-49ae-967e-ae72fb413fc2"
|
||||
authors = ["Enrico Lumetti <enrico.lumetti@gmail.com>"]
|
||||
version = "0.1.0"
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 572d8e50-157c-11eb-180b-71b9ec6cecd1
|
||||
# floating point division
|
||||
3/2
|
||||
|
||||
# ╔═╡ 5e228594-157c-11eb-0554-132854b9b2fd
|
||||
typeof(3/2)
|
||||
|
||||
# ╔═╡ 65f52060-157c-11eb-0511-d5bd5e859ac3
|
||||
# integer division
|
||||
3 ÷ 2 == div(3,2)
|
||||
|
||||
# ╔═╡ b54e5e42-157c-11eb-3aba-57905396206d
|
||||
234 % 13 == rem(234, 13)
|
||||
|
||||
# ╔═╡ 42932eb8-157d-11eb-1dc7-7f070e37d21c
|
||||
# best to use rem: % has another meaning
|
||||
257 % UInt8 # truncation of binary representation to fit UInt8
|
||||
|
||||
# ╔═╡ 7c81c31e-157d-11eb-0310-970e544301f9
|
||||
UInt8(257)
|
||||
|
||||
# ╔═╡ 98a4b0b0-157d-11eb-2afc-c39975e0b404
|
||||
# complex numbers
|
||||
1+3im
|
||||
|
||||
# ╔═╡ a0274b40-157d-11eb-28c5-7961c9a08770
|
||||
typeof(im), typeof(1e2im)
|
||||
|
||||
# ╔═╡ e3386d42-157d-11eb-10ed-116fe8f3606e
|
||||
exp(100+pi*1im)
|
||||
|
||||
# ╔═╡ 21327e58-157e-11eb-072a-79dc893f465f
|
||||
# exact fractionals
|
||||
1//2 + 2//3
|
||||
|
||||
# ╔═╡ 2b65cb78-157e-11eb-258a-f333ab2e64e5
|
||||
numerator(3//4), denominator(3//4)
|
||||
|
||||
# ╔═╡ ad1d9312-157e-11eb-31a2-49de58fffdfe
|
||||
typeof(exp)
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═572d8e50-157c-11eb-180b-71b9ec6cecd1
|
||||
# ╠═5e228594-157c-11eb-0554-132854b9b2fd
|
||||
# ╠═65f52060-157c-11eb-0511-d5bd5e859ac3
|
||||
# ╠═b54e5e42-157c-11eb-3aba-57905396206d
|
||||
# ╠═42932eb8-157d-11eb-1dc7-7f070e37d21c
|
||||
# ╠═7c81c31e-157d-11eb-0310-970e544301f9
|
||||
# ╠═98a4b0b0-157d-11eb-2afc-c39975e0b404
|
||||
# ╠═a0274b40-157d-11eb-28c5-7961c9a08770
|
||||
# ╠═e3386d42-157d-11eb-10ed-116fe8f3606e
|
||||
# ╠═21327e58-157e-11eb-072a-79dc893f465f
|
||||
# ╠═2b65cb78-157e-11eb-258a-f333ab2e64e5
|
||||
# ╠═ad1d9312-157e-11eb-31a2-49de58fffdfe
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.6
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 460801f2-157a-11eb-3658-9d307bab4389
|
||||
a = [1, 2, 3] # vectors need commas; without it's a matrix
|
||||
|
||||
# ╔═╡ 9598a998-170e-11eb-121d-bd32a8b635e8
|
||||
A = [1 2 3]
|
||||
|
||||
# ╔═╡ 99361c56-157a-11eb-358b-052df821fa7a
|
||||
supertypes(typeof(a))
|
||||
|
||||
# ╔═╡ de0fe940-170c-11eb-3d9f-ef06b9e2e5cc
|
||||
AbstractArray{Int, 1} == AbstractVector{Int}
|
||||
|
||||
# ╔═╡ 1c6f0554-170d-11eb-2a68-8781698b424a
|
||||
AbstractArray{Int, 2} == AbstractMatrix{Int}
|
||||
|
||||
# ╔═╡ 64ce9d96-170d-11eb-3ad4-750514d8b1e5
|
||||
supertypes(Matrix{Int})
|
||||
|
||||
# ╔═╡ 6a7549a2-170d-11eb-0c93-956b6767716a
|
||||
AbstractMatrix{Int} in supertypes(Matrix{Int})
|
||||
|
||||
# ╔═╡ af3d6d8a-170d-11eb-090a-6156e3fe68f3
|
||||
# in Julia vector and scalars are different
|
||||
c = 1
|
||||
|
||||
# ╔═╡ fcfeba42-170d-11eb-0adf-47738ff9966f
|
||||
v = [1]
|
||||
|
||||
# ╔═╡ a94656e0-170e-11eb-349c-6f67df33b694
|
||||
M = [1 2; 3 4]
|
||||
|
||||
# ╔═╡ d1ea4808-170d-11eb-0da3-85546b4a84cb
|
||||
typeof(c), typeof(v), typeof(M)
|
||||
|
||||
# ╔═╡ e5ac61ae-170e-11eb-3333-2502ddcbe665
|
||||
# ndims, size. work like in matlab
|
||||
ndims(c), ndims(v), ndims(M)
|
||||
|
||||
# ╔═╡ f60f9c78-170e-11eb-2153-a3b8da1bc465
|
||||
size(c), size(v), size(M)
|
||||
|
||||
# ╔═╡ cda89e3e-1717-11eb-39d5-fb6f72356405
|
||||
# length is equivalent to Matlab's numel, and also works on scalars
|
||||
length(c), length(v), length(M)
|
||||
|
||||
# ╔═╡ 04713bc8-170f-11eb-3da7-ddfd3f4d8cfe
|
||||
# except that size() returns a tuple
|
||||
typeof(size(M))
|
||||
|
||||
# ╔═╡ 8a2a5e40-1712-11eb-0a43-8d18e6f18da4
|
||||
# indexing an element produces a scalar, contrary to matlab
|
||||
M[1, 2]
|
||||
|
||||
# ╔═╡ b44d27ee-1c66-11eb-17e9-754563299763
|
||||
# indexing a full row or column always returns a 1 dimensional array
|
||||
M[1, :]
|
||||
|
||||
# ╔═╡ ca9e20b0-1712-11eb-15e0-93bbc100efa0
|
||||
a[1]
|
||||
|
||||
# ╔═╡ f4908192-1712-11eb-20d8-97d992c4fc0d
|
||||
# difference from matlab: vectors are column vectors by default:
|
||||
# it becomes apparent by transposing (result is two-dimensional)
|
||||
transpose(a)
|
||||
|
||||
# ╔═╡ 81e6f900-1717-11eb-03e7-15e486b854d9
|
||||
#adjoint is
|
||||
[1+im, 3]' # or (adjoint a)
|
||||
|
||||
# ╔═╡ 848dd826-1ba0-11eb-1222-f5b9be57de44
|
||||
# create an array of undefined elements
|
||||
ten_integers = Array{Int}(undef, 10)
|
||||
|
||||
# ╔═╡ a44f73a2-1ba0-11eb-3199-7703ad587000
|
||||
# create an array of ten elements initalized to 3
|
||||
ten_3 = fill(Int32(3), 10)
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═460801f2-157a-11eb-3658-9d307bab4389
|
||||
# ╠═9598a998-170e-11eb-121d-bd32a8b635e8
|
||||
# ╠═99361c56-157a-11eb-358b-052df821fa7a
|
||||
# ╠═de0fe940-170c-11eb-3d9f-ef06b9e2e5cc
|
||||
# ╠═1c6f0554-170d-11eb-2a68-8781698b424a
|
||||
# ╠═64ce9d96-170d-11eb-3ad4-750514d8b1e5
|
||||
# ╠═6a7549a2-170d-11eb-0c93-956b6767716a
|
||||
# ╠═af3d6d8a-170d-11eb-090a-6156e3fe68f3
|
||||
# ╠═fcfeba42-170d-11eb-0adf-47738ff9966f
|
||||
# ╠═a94656e0-170e-11eb-349c-6f67df33b694
|
||||
# ╠═d1ea4808-170d-11eb-0da3-85546b4a84cb
|
||||
# ╠═e5ac61ae-170e-11eb-3333-2502ddcbe665
|
||||
# ╠═f60f9c78-170e-11eb-2153-a3b8da1bc465
|
||||
# ╠═cda89e3e-1717-11eb-39d5-fb6f72356405
|
||||
# ╠═04713bc8-170f-11eb-3da7-ddfd3f4d8cfe
|
||||
# ╠═8a2a5e40-1712-11eb-0a43-8d18e6f18da4
|
||||
# ╠═b44d27ee-1c66-11eb-17e9-754563299763
|
||||
# ╠═ca9e20b0-1712-11eb-15e0-93bbc100efa0
|
||||
# ╠═f4908192-1712-11eb-20d8-97d992c4fc0d
|
||||
# ╠═81e6f900-1717-11eb-03e7-15e486b854d9
|
||||
# ╠═848dd826-1ba0-11eb-1222-f5b9be57de44
|
||||
# ╠═a44f73a2-1ba0-11eb-3199-7703ad587000
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ cacee1ec-19ee-11eb-1437-21d9da5819b4
|
||||
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═cacee1ec-19ee-11eb-1437-21d9da5819b4
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.6
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ e85d0b96-1a4d-11eb-1646-dbb181770804
|
||||
# multiple nested loops
|
||||
res = let # note: let is needed; begin would make res global
|
||||
# and for would not be able to access it
|
||||
res = ""
|
||||
for i=1:3, j=2:3
|
||||
res *= "$i, $j\n"
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═e85d0b96-1a4d-11eb-1646-dbb181770804
|
||||
|
|
@ -0,0 +1,209 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ d9c36a12-19eb-11eb-26a5-87ec7d147a91
|
||||
# basic function declaration
|
||||
function f(x, y)
|
||||
x + y*2 # return is redundant
|
||||
end
|
||||
|
||||
# ╔═╡ eef668d0-19eb-11eb-0ff6-890a1f5753d5
|
||||
f(3, 4)
|
||||
|
||||
# ╔═╡ f515eeaa-19eb-11eb-01bd-31d905ed66ce
|
||||
# function declaration in assignment form
|
||||
g(x) = f(x, x) + 2
|
||||
|
||||
# ╔═╡ 0a693f8c-19ec-11eb-356b-e58715e5bdb7
|
||||
g(1)
|
||||
|
||||
# ╔═╡ 12deb0b4-19ec-11eb-23be-b9b2595b4cc1
|
||||
# g can be used with different types:
|
||||
g(3.4)
|
||||
|
||||
# ╔═╡ 49184494-19ec-11eb-1f62-e7b132947ad0
|
||||
# functions with no return value return "nothing"
|
||||
function procedure()
|
||||
# do stuff
|
||||
end
|
||||
|
||||
# ╔═╡ 4f44b014-19ec-11eb-3356-1b861412d1ed
|
||||
a = procedure()
|
||||
|
||||
# ╔═╡ 526bc2f0-19ec-11eb-0942-c36c57b69cf9
|
||||
a == nothing
|
||||
|
||||
# ╔═╡ 898cfe34-19ec-11eb-13c6-1707cdda1401
|
||||
# operators are functions
|
||||
+(3, 4, 11)
|
||||
|
||||
# ╔═╡ 8d47b60e-19ec-11eb-3826-e9a3a34b5804
|
||||
# anonymous functions
|
||||
h = x -> 2x^2 + 1
|
||||
|
||||
# ╔═╡ a8e5665e-19ec-11eb-1dbe-19658c29b0e2
|
||||
h(3)
|
||||
|
||||
# ╔═╡ b9534f4c-19ec-11eb-0c3e-3d4056a6715a
|
||||
map(x -> 3x, [1, 2, 3])
|
||||
|
||||
# ╔═╡ f86ba81e-19ec-11eb-3ba3-f5b1fa9dafab
|
||||
# argument destructuring on function arguments
|
||||
function sum_tuples((a, b), (c, d))
|
||||
(a+c, b+d)
|
||||
end
|
||||
|
||||
# ╔═╡ 0ef8dbc4-19ed-11eb-15ad-19a4a4e2fdcb
|
||||
sum_tuples((2, 3), (4, 5))
|
||||
|
||||
# ╔═╡ 1f85be8a-19ed-11eb-1285-13351bf00e18
|
||||
# variadic functions
|
||||
function sum_values(x...)
|
||||
res = 0
|
||||
for v in x
|
||||
res += v
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
# ╔═╡ 2f98ebf8-19ed-11eb-1efa-c1334092321d
|
||||
sum_values(), sum_values(2, 3, 4)
|
||||
|
||||
# ╔═╡ 4d263676-19ed-11eb-2a1e-a10c85657e15
|
||||
# pass array or tuple to variadic function
|
||||
v = 1:3
|
||||
|
||||
# ╔═╡ 59fbb2c2-19ed-11eb-379c-bd96b4c82d94
|
||||
sum_values(v...)
|
||||
|
||||
# ╔═╡ 5dd8774a-19ed-11eb-21eb-fdc522cbfd4c
|
||||
tup = (3, 4, 5)
|
||||
|
||||
# ╔═╡ 60698224-19ed-11eb-33bc-0f664ff59ae8
|
||||
sum_values(tup...)
|
||||
|
||||
# ╔═╡ d7185526-19ed-11eb-3029-d7fdf731b05a
|
||||
# also works with non-variadic functions
|
||||
sum_two(a, b) = a+b
|
||||
|
||||
# ╔═╡ e203d406-19ed-11eb-3d1b-2b2d9f774d80
|
||||
sum_two(v[1:2]...)
|
||||
|
||||
# ╔═╡ 65afeed0-19ed-11eb-1db9-65639556c2ae
|
||||
# variadic function with mandatory arguments
|
||||
function varf(a, b...)
|
||||
return a + sum_values(b...)
|
||||
end
|
||||
|
||||
# ╔═╡ 726e9234-19ed-11eb-34b4-35052d906c63
|
||||
varf()
|
||||
|
||||
# ╔═╡ 760d9dcc-19ed-11eb-00ab-2f60dfe89a79
|
||||
varf(1,3)
|
||||
|
||||
# ╔═╡ 0b3ed5c8-19ee-11eb-2d29-952170beda98
|
||||
# keywoard arguments
|
||||
|
||||
|
||||
# ╔═╡ 36127bba-19ee-11eb-001a-2b73fa18afe5
|
||||
# do-block syntax
|
||||
function my_map(f::Function, iterable)
|
||||
res = []
|
||||
for v in iterable
|
||||
push!(res, f(v))
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
# ╔═╡ db3320b8-19f3-11eb-2c88-750633c05917
|
||||
my_map(x ->
|
||||
if x % 2 == 0
|
||||
true
|
||||
else
|
||||
false
|
||||
end, [1,2,3,4])
|
||||
|
||||
# ╔═╡ 1cfccdfa-19f4-11eb-0872-d37643e7945e
|
||||
my_map(x ->
|
||||
begin
|
||||
if x % 2 == 0
|
||||
a = 1
|
||||
else
|
||||
a = 2
|
||||
end
|
||||
a^2
|
||||
end, [1,2,3,4])
|
||||
|
||||
# ╔═╡ 357feaba-19f4-11eb-2d43-597d72fbe61b
|
||||
# same code as above but with do noation
|
||||
my_map([1,2,3,4]) do x
|
||||
if x % 2 == 0
|
||||
a = 1
|
||||
else
|
||||
a = 2
|
||||
end
|
||||
a^2
|
||||
end
|
||||
|
||||
# ╔═╡ d687055e-19fb-11eb-07c9-6b8e79818b43
|
||||
# function composition
|
||||
# operator is \circle
|
||||
square(x)=x^2
|
||||
|
||||
# ╔═╡ fe26c91e-19fb-11eb-3eea-f7311e4da38a
|
||||
times_two(x)=2x
|
||||
|
||||
# ╔═╡ fcd9f6f8-19fb-11eb-1d8b-af0425c4918e
|
||||
map(square ∘ times_two, [1,2,3])
|
||||
|
||||
# ╔═╡ 1a39fe3e-1a10-11eb-2bf7-275b5206b4e0
|
||||
# same as above with dot operator
|
||||
(square ∘ times_two).([1,2,3])
|
||||
|
||||
# ╔═╡ 818a3b74-1a10-11eb-14a0-6d4a364120ad
|
||||
# pipeline operator
|
||||
2 |> square |> times_two # equivalent to (times_two ∘ square)(2)
|
||||
|
||||
# ╔═╡ ebd2f64a-1a12-11eb-2b8d-bbcaf8cea615
|
||||
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═d9c36a12-19eb-11eb-26a5-87ec7d147a91
|
||||
# ╠═eef668d0-19eb-11eb-0ff6-890a1f5753d5
|
||||
# ╠═f515eeaa-19eb-11eb-01bd-31d905ed66ce
|
||||
# ╠═0a693f8c-19ec-11eb-356b-e58715e5bdb7
|
||||
# ╠═12deb0b4-19ec-11eb-23be-b9b2595b4cc1
|
||||
# ╠═49184494-19ec-11eb-1f62-e7b132947ad0
|
||||
# ╠═4f44b014-19ec-11eb-3356-1b861412d1ed
|
||||
# ╠═526bc2f0-19ec-11eb-0942-c36c57b69cf9
|
||||
# ╠═898cfe34-19ec-11eb-13c6-1707cdda1401
|
||||
# ╠═8d47b60e-19ec-11eb-3826-e9a3a34b5804
|
||||
# ╠═a8e5665e-19ec-11eb-1dbe-19658c29b0e2
|
||||
# ╠═b9534f4c-19ec-11eb-0c3e-3d4056a6715a
|
||||
# ╠═f86ba81e-19ec-11eb-3ba3-f5b1fa9dafab
|
||||
# ╠═0ef8dbc4-19ed-11eb-15ad-19a4a4e2fdcb
|
||||
# ╠═1f85be8a-19ed-11eb-1285-13351bf00e18
|
||||
# ╠═2f98ebf8-19ed-11eb-1efa-c1334092321d
|
||||
# ╠═4d263676-19ed-11eb-2a1e-a10c85657e15
|
||||
# ╠═59fbb2c2-19ed-11eb-379c-bd96b4c82d94
|
||||
# ╠═5dd8774a-19ed-11eb-21eb-fdc522cbfd4c
|
||||
# ╠═60698224-19ed-11eb-33bc-0f664ff59ae8
|
||||
# ╠═d7185526-19ed-11eb-3029-d7fdf731b05a
|
||||
# ╠═e203d406-19ed-11eb-3d1b-2b2d9f774d80
|
||||
# ╠═65afeed0-19ed-11eb-1db9-65639556c2ae
|
||||
# ╠═726e9234-19ed-11eb-34b4-35052d906c63
|
||||
# ╠═760d9dcc-19ed-11eb-00ab-2f60dfe89a79
|
||||
# ╠═0b3ed5c8-19ee-11eb-2d29-952170beda98
|
||||
# ╠═36127bba-19ee-11eb-001a-2b73fa18afe5
|
||||
# ╠═db3320b8-19f3-11eb-2c88-750633c05917
|
||||
# ╠═1cfccdfa-19f4-11eb-0872-d37643e7945e
|
||||
# ╠═357feaba-19f4-11eb-2d43-597d72fbe61b
|
||||
# ╠═d687055e-19fb-11eb-07c9-6b8e79818b43
|
||||
# ╠═fe26c91e-19fb-11eb-3eea-f7311e4da38a
|
||||
# ╠═fcd9f6f8-19fb-11eb-1d8b-af0425c4918e
|
||||
# ╠═1a39fe3e-1a10-11eb-2bf7-275b5206b4e0
|
||||
# ╠═818a3b74-1a10-11eb-14a0-6d4a364120ad
|
||||
# ╟─ebd2f64a-1a12-11eb-2b8d-bbcaf8cea615
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 6ee54308-19ee-11eb-33e5-75686f99add0
|
||||
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═6ee54308-19ee-11eb-33e5-75686f99add0
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ e3714204-171a-11eb-26fa-49ac37906267
|
||||
A = [1 2 3; 4 5 6; 0 -1 -2]
|
||||
|
||||
# ╔═╡ d6eae3c0-1772-11eb-2201-1b65698a58ec
|
||||
# first column
|
||||
A[:,1]
|
||||
|
||||
# ╔═╡ e4c54bde-1772-11eb-2294-8f1518fc1fa8
|
||||
# second row
|
||||
A[2, :]
|
||||
|
||||
# ╔═╡ 102fd8de-1773-11eb-190e-f996b16a142f
|
||||
# first and third column
|
||||
A[:, [1, 3]]
|
||||
|
||||
# ╔═╡ 4615b194-1773-11eb-3e57-d73209a2d678
|
||||
# linearize
|
||||
A[:]
|
||||
|
||||
# ╔═╡ 4c962cba-1773-11eb-0282-dd37ec1e6e34
|
||||
# copy
|
||||
A[:,:]
|
||||
|
||||
# ╔═╡ 63b4a638-1773-11eb-3996-afd85f3cf84d
|
||||
a = [1,2,3]
|
||||
|
||||
# ╔═╡ 6c35c5b0-1773-11eb-221e-ab614573e9f1
|
||||
b = [3, 2, 1]
|
||||
|
||||
# ╔═╡ 6f0e4372-1773-11eb-21c8-699fc4b08e30
|
||||
# concatenate a and b horizontally
|
||||
B = [a b]
|
||||
|
||||
# ╔═╡ 98d3921e-1773-11eb-1142-81d5c5810c73
|
||||
hcat(a, b) # same as above
|
||||
|
||||
# ╔═╡ 8311bde0-1773-11eb-03f4-475d1d442217
|
||||
C = [transpose(a); transpose(b)]
|
||||
|
||||
# ╔═╡ ab0133a8-1773-11eb-2146-73b3545d2535
|
||||
vcat(a', b')
|
||||
|
||||
# ╔═╡ ce434a54-1773-11eb-1d87-31d162118b6e
|
||||
# or...
|
||||
hvcat(3, a..., b...) # a and b are expanded
|
||||
|
||||
# ╔═╡ f65962bc-1773-11eb-2633-7d309d3f9b0d
|
||||
# also works with matrices:
|
||||
[A B]
|
||||
|
||||
# ╔═╡ 03951c1e-1774-11eb-1d2e-d91a717a1b05
|
||||
[A; C]
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═e3714204-171a-11eb-26fa-49ac37906267
|
||||
# ╠═d6eae3c0-1772-11eb-2201-1b65698a58ec
|
||||
# ╠═e4c54bde-1772-11eb-2294-8f1518fc1fa8
|
||||
# ╠═102fd8de-1773-11eb-190e-f996b16a142f
|
||||
# ╠═4615b194-1773-11eb-3e57-d73209a2d678
|
||||
# ╠═4c962cba-1773-11eb-0282-dd37ec1e6e34
|
||||
# ╠═63b4a638-1773-11eb-3996-afd85f3cf84d
|
||||
# ╠═6c35c5b0-1773-11eb-221e-ab614573e9f1
|
||||
# ╠═6f0e4372-1773-11eb-21c8-699fc4b08e30
|
||||
# ╠═98d3921e-1773-11eb-1142-81d5c5810c73
|
||||
# ╠═8311bde0-1773-11eb-03f4-475d1d442217
|
||||
# ╠═ab0133a8-1773-11eb-2146-73b3545d2535
|
||||
# ╠═ce434a54-1773-11eb-1d87-31d162118b6e
|
||||
# ╠═f65962bc-1773-11eb-2633-7d309d3f9b0d
|
||||
# ╠═03951c1e-1774-11eb-1d2e-d91a717a1b05
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ f041acdc-1574-11eb-27c4-f56dcea13ce0
|
||||
## README: check type of something
|
||||
typeof(3) == Int64
|
||||
|
||||
# ╔═╡ 83edd348-1575-11eb-222f-cba3bd3e46d3
|
||||
typeof(2e1) == Float64
|
||||
|
||||
# ╔═╡ b87e3576-1575-11eb-11b0-d7319ac3ae81
|
||||
typeof(2f1) == Float32
|
||||
|
||||
# ╔═╡ cbff54c0-1577-11eb-3623-558afada2ee2
|
||||
typemin(Int128), typemax(Int128)
|
||||
|
||||
# ╔═╡ de633a26-1579-11eb-14b2-71b582182fbd
|
||||
supertypes(Int64)
|
||||
|
||||
# ╔═╡ 1ab23fe0-157a-11eb-0489-238fbf08598c
|
||||
subtypes(Int64)
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═f041acdc-1574-11eb-27c4-f56dcea13ce0
|
||||
# ╠═83edd348-1575-11eb-222f-cba3bd3e46d3
|
||||
# ╠═b87e3576-1575-11eb-11b0-d7319ac3ae81
|
||||
# ╠═cbff54c0-1577-11eb-3623-558afada2ee2
|
||||
# ╠═de633a26-1579-11eb-14b2-71b582182fbd
|
||||
# ╠═1ab23fe0-157a-11eb-0489-238fbf08598c
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.6
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ cef9e1f8-1b9f-11eb-1572-d776b0dd8817
|
||||
arr_while = let
|
||||
arr = Array{Function}(undef, 3)
|
||||
i = 1
|
||||
while i <= length(arr)
|
||||
arr[i] = ()->i
|
||||
i += 1
|
||||
end
|
||||
arr
|
||||
end
|
||||
|
||||
# ╔═╡ 3d9ec092-1ba0-11eb-29b8-63b3f1e75a13
|
||||
# the while loop "i" is capture in all the closures and is the same for all of them
|
||||
arr_while[1](), arr_while[2]()
|
||||
|
||||
# ╔═╡ 651d6c0e-1ba0-11eb-2af8-1b1784dbe9b1
|
||||
arr_for = let
|
||||
arr = Array{Function}(undef, 4)
|
||||
for i=1:length(arr)
|
||||
arr[i] = ()->i
|
||||
end
|
||||
arr
|
||||
end
|
||||
|
||||
# ╔═╡ 4b602bfc-1ba1-11eb-12a9-e59a34c1eaae
|
||||
# the captured i is different here: for allocates a new variable each time
|
||||
arr_for[2](), arr_for[3]()
|
||||
|
||||
# ╔═╡ 7a853616-1ba1-11eb-3ec2-b116cb4e08d3
|
||||
# reuse of variables as loop index
|
||||
begin
|
||||
local i = 0
|
||||
for outer i in 1:4
|
||||
#...
|
||||
end
|
||||
|
||||
local msg :: String
|
||||
for outer msg in ["Hello", "World"]
|
||||
#...
|
||||
end
|
||||
|
||||
i, msg
|
||||
end
|
||||
|
||||
# ╔═╡ 2941d9fc-1ba2-11eb-0edb-eb46ed882aa0
|
||||
# constants
|
||||
const a = 3
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═cef9e1f8-1b9f-11eb-1572-d776b0dd8817
|
||||
# ╠═3d9ec092-1ba0-11eb-29b8-63b3f1e75a13
|
||||
# ╠═651d6c0e-1ba0-11eb-2af8-1b1784dbe9b1
|
||||
# ╠═4b602bfc-1ba1-11eb-12a9-e59a34c1eaae
|
||||
# ╠═7a853616-1ba1-11eb-3ec2-b116cb4e08d3
|
||||
# ╠═2941d9fc-1ba2-11eb-0edb-eb46ed882aa0
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 7b294e34-19eb-11eb-1e57-d76e0cd1c3cd
|
||||
# string interpolation
|
||||
name = "Enrico"
|
||||
|
||||
# ╔═╡ a373b3c0-19eb-11eb-2b92-a5998a81e85e
|
||||
msg = "Hello"
|
||||
|
||||
# ╔═╡ a5a7e4b8-19eb-11eb-361e-ff718e1adbd1
|
||||
|
||||
|
||||
# ╔═╡ 93f27990-19eb-11eb-1abe-3d082300d218
|
||||
"$msg $(name)!"
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═7b294e34-19eb-11eb-1e57-d76e0cd1c3cd
|
||||
# ╠═a373b3c0-19eb-11eb-2b92-a5998a81e85e
|
||||
# ╠═a5a7e4b8-19eb-11eb-361e-ff718e1adbd1
|
||||
# ╠═93f27990-19eb-11eb-1abe-3d082300d218
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ cd0abcc0-171a-11eb-3b5c-4d433f9affbc
|
||||
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═cd0abcc0-171a-11eb-3b5c-4d433f9affbc
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.4
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 347f99b2-18be-11eb-2ee8-f92ec1a3be6f
|
||||
Σ = [1 2 3; 4 5 6]
|
||||
|
||||
# ╔═╡ c10eced4-1774-11eb-151a-21f1a1c60225
|
||||
Σ_float = convert(Matrix{Float32}, Σ)
|
||||
|
||||
# ╔═╡ ee1b09c8-196e-11eb-2362-37b9901ade27
|
||||
Σ_float[1, 2] = 93
|
||||
|
||||
# ╔═╡ fc1de1da-196e-11eb-33cd-23c342307d56
|
||||
# the above cell is equal to this:
|
||||
Σ_float[1, 2] = convert(Float32, 93)
|
||||
# convert is called implicitly in other context: see the manual
|
||||
# custom behavior for convert is possible
|
||||
|
||||
# ╔═╡ 2d3efb3a-1971-11eb-3079-f148a705b630
|
||||
Base.float(Σ) # helper function, but returns only Float64
|
||||
|
||||
# ╔═╡ b193ef8c-196f-11eb-08d3-f9ecd7a90907
|
||||
Σ_float
|
||||
|
||||
# ╔═╡ c9e251a2-1970-11eb-2096-a9bdd1c39478
|
||||
# convert string to numerical type
|
||||
parse(Int, "3")
|
||||
|
||||
# ╔═╡ 128f4e3e-1971-11eb-3d6e-1f346839cf41
|
||||
parse(Float64, "3.5")
|
||||
|
||||
# ╔═╡ 1b4e8558-1971-11eb-3486-63f5b0ee6333
|
||||
# convert integer to string in the specified base
|
||||
Base.string(14, base = 16)
|
||||
|
||||
# ╔═╡ ec19bf68-1971-11eb-3bbd-21a42ac455d4
|
||||
# convert multiple arguments to a string (not just integers)
|
||||
Base.string("Hello: ", 1.3, ' ', [1 2])
|
||||
|
||||
# ╔═╡ 0ebb8178-1972-11eb-32d4-f144eeb29db1
|
||||
# string concatenation
|
||||
"Hello " * " World!"
|
||||
|
||||
# ╔═╡ 544ef468-1972-11eb-2b5b-f5e9323069b8
|
||||
# gives the string representation of a value using the show() function (which prints to IO)
|
||||
repr([1 2; 3 4])
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═347f99b2-18be-11eb-2ee8-f92ec1a3be6f
|
||||
# ╠═c10eced4-1774-11eb-151a-21f1a1c60225
|
||||
# ╠═ee1b09c8-196e-11eb-2362-37b9901ade27
|
||||
# ╠═fc1de1da-196e-11eb-33cd-23c342307d56
|
||||
# ╠═2d3efb3a-1971-11eb-3079-f148a705b630
|
||||
# ╠═b193ef8c-196f-11eb-08d3-f9ecd7a90907
|
||||
# ╠═c9e251a2-1970-11eb-2096-a9bdd1c39478
|
||||
# ╠═128f4e3e-1971-11eb-3d6e-1f346839cf41
|
||||
# ╠═1b4e8558-1971-11eb-3486-63f5b0ee6333
|
||||
# ╠═ec19bf68-1971-11eb-3bbd-21a42ac455d4
|
||||
# ╠═0ebb8178-1972-11eb-32d4-f144eeb29db1
|
||||
# ╠═544ef468-1972-11eb-2b5b-f5e9323069b8
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
### A Pluto.jl notebook ###
|
||||
# v0.12.6
|
||||
|
||||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# ╔═╡ 57970f5a-1ba2-11eb-2e5e-c150bd730b65
|
||||
struct Foo
|
||||
bar # Any
|
||||
baz::Int
|
||||
qux::Float64
|
||||
end
|
||||
|
||||
# ╔═╡ f156f116-1ba2-11eb-2d84-f9f02380f42d
|
||||
typeof(Foo)
|
||||
|
||||
# ╔═╡ 7ff7efd8-1ba3-11eb-1247-7d69a45fca1d
|
||||
fieldnames(Foo)
|
||||
|
||||
# ╔═╡ 8b309210-1ba3-11eb-33a1-977e8fc172d2
|
||||
fieldtypes(Foo)
|
||||
|
||||
# ╔═╡ f5c339a8-1ba2-11eb-3d75-dfa770226a9b
|
||||
f = Foo(0, 1, 3.4)
|
||||
|
||||
# ╔═╡ f7760d60-1ba3-11eb-2bb6-85ea174dcd7f
|
||||
f.qux
|
||||
|
||||
# ╔═╡ 294b331e-1ba3-11eb-23b1-1befbcafd110
|
||||
# bar is not Any but a specific type
|
||||
map(typeof, (f.bar, f.baz, f.qux))
|
||||
|
||||
# ╔═╡ 7e1b1b9a-1ba3-11eb-0ac9-23f4f325e05c
|
||||
# Foo is not assignable
|
||||
f.qux = 3.8
|
||||
|
||||
# ╔═╡ 198e17c6-1ba4-11eb-3af2-f3f8b1180eb8
|
||||
mutable struct BankAccount
|
||||
money :: Int
|
||||
end
|
||||
|
||||
# ╔═╡ 3abe2a5a-1ba4-11eb-0830-3d7c2e889e31
|
||||
bk = BankAccount(3)
|
||||
|
||||
# ╔═╡ 46274816-1ba4-11eb-08a3-47bc4a9a92bd
|
||||
bk.money += 10
|
||||
|
||||
# ╔═╡ b26554dc-1ba4-11eb-30cb-9f2c5900042d
|
||||
# union types = haskell datatypes
|
||||
MaybeString = Union{Int, Nothing}
|
||||
|
||||
# ╔═╡ 331e0f08-1ba5-11eb-3d57-ab4f856042fb
|
||||
s = 3::MaybeString
|
||||
|
||||
# ╔═╡ 39908440-1ba5-11eb-3360-d10fcbc18b56
|
||||
typeof(s)
|
||||
|
||||
# ╔═╡ 7010b83a-1bb1-11eb-19ca-15f4270b7597
|
||||
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═57970f5a-1ba2-11eb-2e5e-c150bd730b65
|
||||
# ╠═f156f116-1ba2-11eb-2d84-f9f02380f42d
|
||||
# ╠═7ff7efd8-1ba3-11eb-1247-7d69a45fca1d
|
||||
# ╠═8b309210-1ba3-11eb-33a1-977e8fc172d2
|
||||
# ╠═f5c339a8-1ba2-11eb-3d75-dfa770226a9b
|
||||
# ╠═f7760d60-1ba3-11eb-2bb6-85ea174dcd7f
|
||||
# ╠═294b331e-1ba3-11eb-23b1-1befbcafd110
|
||||
# ╠═7e1b1b9a-1ba3-11eb-0ac9-23f4f325e05c
|
||||
# ╠═198e17c6-1ba4-11eb-3af2-f3f8b1180eb8
|
||||
# ╠═3abe2a5a-1ba4-11eb-0830-3d7c2e889e31
|
||||
# ╠═46274816-1ba4-11eb-08a3-47bc4a9a92bd
|
||||
# ╠═b26554dc-1ba4-11eb-30cb-9f2c5900042d
|
||||
# ╠═331e0f08-1ba5-11eb-3d57-ab4f856042fb
|
||||
# ╠═39908440-1ba5-11eb-3360-d10fcbc18b56
|
||||
# ╠═7010b83a-1bb1-11eb-19ca-15f4270b7597
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
module Aristarkos
|
||||
|
||||
include("matrix.jl")
|
||||
|
||||
end # module
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
|
||||
"""
|
||||
multiply two matrices A and B=[b1|...|bn] as [A*b1|...|A*bn]
|
||||
"""
|
||||
function matmul_col(A::AbstractMatrix{<:Real}, B::AbstractMatrix{<:Real})
|
||||
if size(A, 2) != size(B, 1)
|
||||
error("Cannot multiply A of size $(size(A)) and B of size $(size(B))")
|
||||
end
|
||||
|
||||
T1 = eltype(A)
|
||||
T2 = eltype(B)
|
||||
res = Matrix{promote_type(T1,T2)}(undef, size(A, 1), size(B, 2))
|
||||
for i = 1:size(B, 2)
|
||||
res[:, i] = A*B[:, i]
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
"""
|
||||
multiply two matrices A=[a1; a2; ...;an] and B as [a1*B;...;an*B]
|
||||
"""
|
||||
function matmul_row(A::AbstractMatrix{<:Real}, B::AbstractMatrix{<:Real})
|
||||
if size(A, 2) != size(B, 1)
|
||||
error("Cannot multiply A of size $(size(A)) and B of size $(size(B))")
|
||||
end
|
||||
|
||||
T1 = eltype(A)
|
||||
T2 = eltype(B)
|
||||
res = Matrix{promote_type(T1,T2)}(undef, size(A, 1), size(B, 2))
|
||||
for i = 1:size(A, 1)
|
||||
res[i, :] = transpose(A[i, :])*B
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
"""
|
||||
multiplty two matrix A and B using the element-wise product rule
|
||||
"""
|
||||
function matmul_elementwise(A::AbstractMatrix{<:Real}, B::AbstractMatrix{<:Real})
|
||||
if size(A, 2) != size(B, 1)
|
||||
error("Cannot multiply A of size $(size(A)) and B of size $(size(B))")
|
||||
end
|
||||
|
||||
T1 = eltype(A)
|
||||
T2 = eltype(B)
|
||||
T = promote_type(T1, T2)
|
||||
res = Matrix{T}(undef, size(A, 1), size(B, 2))
|
||||
fill!(res, zero(T))
|
||||
for i=1:size(A, 1), j=1:size(B, 2)
|
||||
res[i, j] += sum(A[i, :] .* B[:, j])
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
Loading…
Reference in New Issue