init l2
This commit is contained in:
parent
2869ca589a
commit
7a407fcfe4
6 changed files with 197 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@
|
||||||
**/*.pdf
|
**/*.pdf
|
||||||
**/*.zip
|
**/*.zip
|
||||||
.direnv/
|
.direnv/
|
||||||
|
julia-fix-gksqt.nix
|
||||||
|
|
23
julia-fix-gksqt.sh
Executable file
23
julia-fix-gksqt.sh
Executable file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
GRDIR=$(julia -e 'using Plots; println(ENV["GRDIR"])' | awk -F'/src' '{print $1}')
|
||||||
|
cat <<EOF > julia-fix-gksqt.nix
|
||||||
|
with import <nixpkgs> {};
|
||||||
|
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
|
1
l2/.gitignore
vendored
Normal file
1
l2/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.png
|
151
l2/1.jl
Executable file
151
l2/1.jl
Executable file
|
@ -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)")
|
7
l2/2.jl
Executable file
7
l2/2.jl
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env julia
|
||||||
|
|
||||||
|
# Jacek Poziemski 272389
|
||||||
|
|
||||||
|
import Pkg
|
||||||
|
Pkg.add("Plots")
|
||||||
|
using Plots
|
14
l2/README.md
Normal file
14
l2/README.md
Normal file
|
@ -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
|
Loading…
Reference in a new issue