juliateachingctu / scientific-programming-in-julia Goto Github PK
View Code? Open in Web Editor NEWRepository for B0M36SPJ
Home Page: https://juliateachingctu.github.io/Scientific-Programming-in-Julia/dev/
License: MIT License
Repository for B0M36SPJ
Home Page: https://juliateachingctu.github.io/Scientific-Programming-in-Julia/dev/
License: MIT License
In homework from week 6, there semms to be a problem with the assignment itself https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia/blob/master/docs/src/lecture_06/hw.md.
There is line:
return an array of unique alphabetically sorted symbols representing variables in an expression.
However next to it is example
[:x, :y, :z, :c]
Which is not alphabetically sorted
module A
f() = println("hello")
macro foo()
:(f())
end
end
Main.A
julia> f() = println("goodbye")
f (generic function with 1 method)
julia> A.@foo
hello
and compare this to
module A
f() = println("hello")
macro foo()
:(esc(f())))
end
end
##1
is not directly accessible. Try f.x and g.x. is I think incorrect as you can access it. Try in replfunction call_counter(f)
called = 0
(args...;kwargs...) -> begin
called += 1
f(args...;kwargs...)
end
end
counted_println = call_counter(println);
counted_println("hello world"
But somehow the above does not trigger that
https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/4
type anotated global
Unit test can be setup also on GitLab, see tutorial from Tamás K. Papp.
It boils down to adding a file .gitlab-ci.yml
to the root of a repo with the following few lines
image: julia:1.6 # image comes from Docker hub
default:
script:
- julia --project=@. -e "import Pkg; Pkg.test()"
Documentation can be hosted in a similar fashion as well.
Add more references to included images.
Small note on NVidia/AMD framework comparison
there is a nice picture which shows the division of work over a 1D/2D array into blocks/threads etc - https://youtu.be/LG9G4aA28rU?t=368 - some analog should be available somewhere or created manually
x property suggest that you can partition the execution along three-dimensional cube (three nested for loops),
- should be explained better add pictures
using GPU without writing kernels should mention the scalar indexing problem, which we often encounter (comparing CPU and GPU array without moving them to the same place, show methods fallback to getindex! ops)
Show them @forward macro, how to write it and what it does.
the code here
function find_food(a::Sheep, w::World)
as = filter(x->isa(x,Grass), w.agents |> values |> collect)
isempty(as) ? nothing : sample(as)
end
filters objects so that only grass is present, but does not check the size of grass, so 0 sheep may try to eat 0 size grass and get 0 energy
Abstraction was too tough, explain better, how things click it.
We should have a naive explanation for a macro when we first use it (probably already in the second lecture).
Homework: custom adjoint of tanh/map
Implement / test / benchmark a parallelized version of the Ecosystem which would be based on multiple phases of an agent step:
⚥Sheep
forward method including overloading of getproperty
(and setproperty!
) for ⚥Sheep
:function Base.getproperty(s::⚥Sheep, x::Symbol)
if x == :sex || x == :sheep
getfield(s,x)
else
getfield(s.sheep,x)
end
end
filter
is both slow and not super straight forward in this case. We could remove hint 1 and expand on hint 2. Scientific-Programming-in-Julia/docs/src/lecture_02/lab.md
Lines 372 to 377 in c77e2a5
EcosystemCore.mates(::Animal{S,Female}, ::Animal{S,Male}) where S<:Species = true
EcosystemCore.mates(::Animal{S,Male}, ::Animal{S,Female}) where S<:Species = true
EcosystemCore.mates(::Agent, ::Agent) = false
########## Additional functionality ##########################################
function simulate!(world::World, iters::Int; cb=()->())
for i in 1:iters
world_step!(world)
cb()
end
end
agent_count(p::Plant) = size(p)/EcosystemCore.max_size(p)
agent_count(::Animal) = 1
agent_count(as::Vector{<:Agent}) = sum(agent_count,as)
function agent_count(w::World)
function op(d::Dict,a::Agent{S}) where S<:Species
n = nameof(S)
if n in keys(d)
d[n] += agent_count(a)
else
d[n] = agent_count(a)
end
return d
end
foldl(op, w.agents |> values |> collect, init=Dict{Symbol,Real}())
end
function every_nth(f::Function, n::Int)
i = 1
function callback(args...)
# display(i) # comment this out to see out the counter increases
if i == n
f(args...)
i = 1
else
i += 1
end
end
end
Use parametric type Sex
The getters and setters we currently use in the Ecosystem are only there because of the ⚥Sheep
type which should demonstrate the forwarding of fields. We could get rid of all of them by overloading getproperty
(and setproperty!
) for ⚥Sheep
instead:
function Base.getproperty(s::⚥Sheep, x::Symbol)
if x == :sex || x == :sheep
getfield(s,x)
else
getfield(s.sheep,x)
end
end
In 2nd lab we are provided with method agent_step!(a::Animal, w::World).
This method assumes we have implmented kill_agent!() method which is then used here
if energy(a) <= 0 kill_agent!(a,w) return end
But this is never referenced as a task or side note, but it is present in solved solutions.
It would be worth to add task for creating this method aswell, so the simulation works in the end after adding all other methods.
All the versions of polynomial
function should share the same order of coefficients, that is consistent with how it is evaluated in Julia's evalpoly
.
Currently in the lab we have coefficients ordered from a_0
to a_n
, but it is probably reversed such that the order from the original polynomial a_n*x^n + a_{n-1}*x^{n-1} + ... + a_1*x + a_0
is conserved.
Affected pages
Scientific-Programming-in-Julia/docs/src/lecture_02/lab.md
Lines 372 to 377 in c77e2a5
Using filter
is both slow and not super straight forward in this case. We could remove hint 1 and expand on hint 2.
In labs, we talk a lot about polynomials as an example where meta programming buys you speed due to loop unrolling.
But students might not appreciate why polynomials are such a big deal.
Test and if it succeeds, put AbbreviatedStackTraces.jl
to Lecture 1 on parsing the errors
We should probably use PkgTemplates.jl
to simplify the lab. Orient on Vasek's workflow tutorial in lecture 7 of the julia for optimization course.
Checking type stability on function with optional arguments does not show the whole picture
@code_warntype find_root(p, Newton(), -5.0, 5.0) # shows only the wrapper function which is stable
@code_warntype find_root(p, Newton(), -5.0, 5.0, 100, 0.95, 1e-12) # fully specified (shows type instabilities - also used in the eval)
The problem here is that the function is type stable in the way that the compiler knows the output type before execution, however the eval pipeline checks for all ::Any
entries in the fully specified function, i.e. for runtime dispatches of which there are few. This leads to confusion based on what has been said in the lab.
Ideally we should have used deepcopy
and fixed Random.seed!
for the simulation profiling.
Different versions of the code might use RNG in a different way, thus the simulation is also different.
The same applies for the benchmarking. We should benchmark only what is needed: find_rand
.
EcosystemCore.mates(::Animal{S,Female}, ::Animal{S,Male}) where S<:Species = true
EcosystemCore.mates(::Animal{S,Male}, ::Animal{S,Female}) where S<:Species = true
EcosystemCore.mates(::Agent, ::Agent) = false
########## Additional functionality ##########################################
function simulate!(world::World, iters::Int; cb=()->())
for i in 1:iters
world_step!(world)
cb()
end
end
agent_count(p::Plant) = size(p)/EcosystemCore.max_size(p)
agent_count(::Animal) = 1
agent_count(as::Vector{<:Agent}) = sum(agent_count,as)
function agent_count(w::World)
function op(d::Dict,a::Agent{S}) where S<:Species
n = nameof(S)
if n in keys(d)
d[n] += agent_count(a)
else
d[n] = agent_count(a)
end
return d
end
foldl(op, w.agents |> values |> collect, init=Dict{Symbol,Real}())
end
function every_nth(f::Function, n::Int)
i = 1
function callback(args...)
# display(i) # comment this out to see out the counter increases
if i == n
f(args...)
i = 1
else
i += 1
end
end
end
Hi, I have a I think correct solution for the string replacement, in case you wish to add it.
sreplace_i(s) = replace(s, r"([^\w]|\b)i(?=[^\w]|\z)" => s"\1k")
It replaces "i" only if it is not preceded or followed by an alphanumeric character or there is an end/beginning of string.
example:
s = "i + i*i + y*i - sin(z) + int(i) + i"
sreplace_i(s)
"k + k*k + y*k - sin(z) + int(k) + k"
ODE variables/coordinates have different names in code and in latex equations...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.