-
Notifications
You must be signed in to change notification settings - Fork 0
/
soe.jl
61 lines (45 loc) · 982 Bytes
/
soe.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using LinearAlgebra
module SystemOfEquations
export generate_soe
letters = split("abcdefghijklmnopqrstuvwxyz", "")
function generate_soe(dims::Int64, size::Int64 = 8) # dims within 1 to 26
function strvar(idx::Int64)::String
k = length(letters) - dims + idx
if dims <= 2
k -= 3 - dims
end
return string(letters[k])
end
coefficients = rand([collect(-size:-1); collect(1:size)], dims, dims)
variables = rand(-size:size, dims)
answers::Vector{Int64} = []
for row in eachrow(coefficients)
sum = 0
for i in 1:dims
sum += row[i] * variables[i]
end
push!(answers, sum)
end
str = ""
for j in 1:dims
for i in 1:dims
k = coefficients[j, i]
if k >= 0 && i != 1
str *= "+"
end
if k == -1
str *= "-"
elseif k != 1
str *= string(k)
end
str *= strvar(i)
end
str *= "=" * string(answers[j]) * "\n"
end
str *= "\n"
for i in 1:dims
str *= strvar(i) * "=" * string(variables[i]) * "\n"
end
return str
end
end