diff --git a/.gitignore b/.gitignore index 51f8e02..6b03278 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ **/*.pdf **/*.zip .direnv/ +julia-fix-gksqt.nix diff --git a/julia-fix-gksqt.sh b/julia-fix-gksqt.sh new file mode 100755 index 0000000..f43f7bf --- /dev/null +++ b/julia-fix-gksqt.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +GRDIR=$(julia -e 'using Plots; println(ENV["GRDIR"])' | awk -F'/src' '{print $1}') +cat < julia-fix-gksqt.nix +with import {}; +let + libPath = lib.makeLibraryPath [ + stdenv.cc.cc.lib qt5.qtbase qt5Full libGL ]; +in + pkgs.stdenv.mkDerivation { + name = "julia-fix-gksqt"; + shellHook = '' + export QT_QPA_PLATFORM_PLUGIN_PATH="\${qt5.qtbase.bin}/lib/qt-\${qt5.qtbase.version}/plugins" + chmod +wx $GRDIR/bin/gksqt + \${patchelf}/bin/patchelf \\ + --set-interpreter \${glibc}/lib/ld-linux-x86-64.so.2 \\ + --set-rpath "\${libPath}" \\ + $GRDIR/bin/gksqt + ldd $GRDIR/bin/gksqt \\ + | grep -q "not found" || \\ + echo '../gksqt is patched' + ''; + } +EOF diff --git a/l2/.gitignore b/l2/.gitignore new file mode 100644 index 0000000..e33609d --- /dev/null +++ b/l2/.gitignore @@ -0,0 +1 @@ +*.png diff --git a/l2/1.jl b/l2/1.jl new file mode 100755 index 0000000..615d802 --- /dev/null +++ b/l2/1.jl @@ -0,0 +1,151 @@ +#!/usr/bin/env julia + +# Jacek Poziemski 272389 + +""" + sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + +Calculate the scalar sum of `x` and `y`, +going from the first element to the last. + +# Arguments +- `x::Vector{T}`: first of the 2 vectors +- `y::Vector{T}`: second of the 2 vectors +""" +function sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + s::T = zero(T) + + for i::Int in 1:(length(x)) + s += x[i] * y[i] + end + + return s +end + +""" + sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + +Calculate the scalar sum of `x` and `y`, +going from the last element to the first. + +# Arguments +- `x::Vector{T}`: first of the 2 vectors +- `y::Vector{T}`: second of the 2 vectors +""" +function sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + s::T = zero(T) + + for i::Int in (length(x)):-1:1 + s += x[i] * y[i] + end + + return s +end + +""" + sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + +Calculate the scalar sum of `x` and `y` by calculating +the partial sums of positive and negative numbers, +summing up the partial sums using a decreasing order +according to their absolute values. + +# Arguments +- `x::Vector{T}`: first of the 2 vectors +- `y::Vector{T}`: second of the 2 vectors +""" +function sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + s::Vector{T} = zeros(T, length(x)) + + for i::Int in 1:(length(x)) + s[i] = x[i] * y[i] + end + + spos::Vector{T} = s[s .> 0] + sort!(spos, rev = true) + + sneg::Vector{T} = s[s .<= 0] + sort!(sneg) + + neg::T = zero(T) + for x::T in sneg + neg += x + end + + pos::T = zero(T) + for x::T in spos + pos += x + end + + return pos + neg +end + +""" + sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + +Calculate the scalar sum of `x` and `y` by calculating +the partial sums of positive and negative numbers, +summing up the partial sums using an increasing order +according to their absolute values. + +# Arguments +- `x::Vector{T}`: first of the 2 vectors +- `y::Vector{T}`: second of the 2 vectors +""" +function sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat + s::Vector{T} = zeros(T, length(x)) + + for i::Int in 1:(length(x)) + s[i] = x[i] * y[i] + end + + spos::Vector{T} = s[s .> 0] + sort!(spos) + + sneg::Vector{T} = s[s .<= 0] + sort!(sneg, rev = true) + + neg::T = zero(T) + for x::T in sneg + neg += x + end + + pos::T = zero(T) + for x::T in spos + pos += x + end + + return pos + neg +end + +x32::Vector{Float32} = [2.718281828, −3.141592654, 1.414213562, 0.577215664, 0.301029995] +y32::Vector{Float32} = [1486.2497, 878366.9879, −22.37492, 4773714.647, 0.000185049] + +x64::Vector{Float64} = [2.718281828, −3.141592654, 1.414213562, 0.577215664, 0.301029995] +y64::Vector{Float64} = [1486.2497, 878366.9879, −22.37492, 4773714.647, 0.000185049] + +expected::Float64 = −1.00657107000000 * 10^(−11) + +res::Float64 = sumvectorsforwards(x32, y32) +println("summing x32 and y32 forwards: $res; correct: $(res == expected)") + +res = sumvectorsbackwards(x32, y32) +println("summing x32 and y32 backwards: $res; correct: $(res == expected)") + +res = sumvectorsdecreasing(x32, y32) +println("summing x32 and y32 decreasing: $res; correct: $(res == expected)") + +res = sumvectorsincreasing(x32, y32) +println("summing x32 and y32 increasing: $res; correct: $(res == expected)") + +res = sumvectorsforwards(x64, y64) +println("summing x64 and y64 forwards: $res; correct: $(res == expected)") + +res = sumvectorsbackwards(x64, y64) +println("summing x64 and y64 backwards: $res; correct: $(res == expected)") + +res = sumvectorsdecreasing(x64, y64) +println("summing x64 and y64 decreasing: $res; correct: $(res == expected)") + +res = sumvectorsincreasing(x64, y64) +println("summing x64 and y64 increasing: $res; correct: $(res == expected)") diff --git a/l2/2.jl b/l2/2.jl new file mode 100755 index 0000000..342f720 --- /dev/null +++ b/l2/2.jl @@ -0,0 +1,7 @@ +#!/usr/bin/env julia + +# Jacek Poziemski 272389 + +import Pkg +Pkg.add("Plots") +using Plots diff --git a/l2/README.md b/l2/README.md new file mode 100644 index 0000000..9818755 --- /dev/null +++ b/l2/README.md @@ -0,0 +1,14 @@ +# julia plots on nixos fix + +script taken from https://gist.github.com/konfou/d12c0a26fc0d3b432dc9d23c86701fcb and modified to actually work + +first run `./2.jl` or any other julia program that adds the +`Plots` package to fetch all the julia packages I presume + +after everything is fetched and you most likely get an error, +go back to the root of the project and run `julia-fix-gksqt.sh` +to generate a nix shell under `julia-fix-gksqt.nix` with the correct +path to the gksqt binary under `~/.julia` (fuck julia for this) + +then enter that nix shell using `nix-shell julia-fix-gksqt.nix` at +which point you should be able to run your julia program with no issues