2024-07-21 13:45:57 +02:00
|
|
|
using CUDA
|
2024-07-14 15:08:05 +02:00
|
|
|
using .ExpressionProcessing
|
|
|
|
using .Interpreter
|
|
|
|
|
2024-07-21 13:45:57 +02:00
|
|
|
expressions = Vector{Expr}(undef, 2)
|
2024-11-01 11:23:58 +01:00
|
|
|
variables = Matrix{Float32}(undef, 2,2)
|
|
|
|
parameters = Vector{Vector{Float32}}(undef, 2)
|
2024-07-14 15:08:05 +02:00
|
|
|
|
2024-07-24 14:42:26 +02:00
|
|
|
# Resulting value should be 10 for the first expression
|
2024-07-14 15:08:05 +02:00
|
|
|
expressions[1] = :(x1 + 1 * x2 + p1)
|
2024-07-21 13:45:57 +02:00
|
|
|
expressions[2] = :(5 + x1 + 1 * x2 + p1 + p2)
|
|
|
|
variables[1,1] = 2.0
|
2024-08-03 12:11:43 +02:00
|
|
|
variables[2,1] = 3.0
|
|
|
|
variables[1,2] = 0.0
|
2024-07-24 14:42:26 +02:00
|
|
|
variables[2,2] = 5.0
|
2024-11-01 11:23:58 +01:00
|
|
|
parameters[1] = Vector{Float32}(undef, 1)
|
|
|
|
parameters[2] = Vector{Float32}(undef, 2)
|
2024-07-21 13:45:57 +02:00
|
|
|
parameters[1][1] = 5.0
|
|
|
|
parameters[2][1] = 5.0
|
|
|
|
parameters[2][2] = 0.0
|
2024-07-14 15:08:05 +02:00
|
|
|
|
2024-11-01 11:23:58 +01:00
|
|
|
function testHelper(expression::Expr, variables::Matrix{Float32}, parameters::Vector{Vector{Float32}}, expectedResult)
|
2024-08-25 13:01:58 +02:00
|
|
|
postfix = Vector([expr_to_postfix(expression)])
|
|
|
|
result = Interpreter.interpret(postfix, variables, parameters)
|
2024-11-01 11:23:58 +01:00
|
|
|
|
|
|
|
expectedResult32 = convert(Float32, expectedResult)
|
|
|
|
@test isequal(result[1,1], expectedResult32)
|
2024-08-25 13:01:58 +02:00
|
|
|
end
|
|
|
|
|
2024-07-21 13:45:57 +02:00
|
|
|
@testset "Test conversion to matrix" begin
|
2024-11-01 11:23:58 +01:00
|
|
|
reference = Matrix{Float32}(undef, 2, 2)
|
2024-07-21 13:45:57 +02:00
|
|
|
reference[1,1] = 5.0
|
2024-11-01 11:23:58 +01:00
|
|
|
reference[2,1] = NaN32
|
2024-07-21 13:45:57 +02:00
|
|
|
reference[1,2] = 5.0
|
|
|
|
reference[2,2] = 0.0
|
|
|
|
# reference = Matrix([5.0, NaN],
|
|
|
|
# [5.0, 0.0])
|
2024-11-01 11:23:58 +01:00
|
|
|
result = Interpreter.convert_to_matrix(parameters, NaN32)
|
2024-07-21 13:45:57 +02:00
|
|
|
|
|
|
|
@test isequal(result, reference)
|
2024-08-08 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2024-08-25 13:01:58 +02:00
|
|
|
@testset "Test commutative interpretation" begin
|
2024-11-01 11:23:58 +01:00
|
|
|
var = Matrix{Float32}(undef, 2, 1)
|
|
|
|
param = Vector{Vector{Float32}}(undef, 1)
|
2024-09-01 12:09:29 +02:00
|
|
|
expectedResult = 8.0 # Not using "eval" because the variables are not stored in global scope
|
2024-08-25 13:01:58 +02:00
|
|
|
|
|
|
|
var[1,1] = 3.0
|
|
|
|
var[2,1] = 5.0
|
|
|
|
param[1] = [3.0, 5.0]
|
|
|
|
|
|
|
|
# test with fixed values
|
|
|
|
expr = :(3.0 + 5.0)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
# test with variables
|
|
|
|
expr = :(x1 + x2)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
# test with parameters
|
|
|
|
expr = :(p1 + p2)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
2024-08-18 11:55:41 +02:00
|
|
|
end
|
2024-08-25 13:01:58 +02:00
|
|
|
|
|
|
|
@testset "Test non commutative interpretation" begin
|
2024-11-01 11:23:58 +01:00
|
|
|
var = Matrix{Float32}(undef, 2, 1)
|
|
|
|
param = Vector{Vector{Float32}}(undef, 1)
|
2024-09-01 12:09:29 +02:00
|
|
|
expectedResult = -2.0 # Not using "eval" because the variables are not stored in global scope
|
2024-08-25 13:01:58 +02:00
|
|
|
|
|
|
|
var[1,1] = 3.0
|
|
|
|
var[2,1] = 5.0
|
|
|
|
param[1] = [3.0, 5.0]
|
|
|
|
|
|
|
|
# test with fixed values
|
|
|
|
expr = :(3.0 - 5.0)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
2024-08-18 11:55:41 +02:00
|
|
|
# One Test with fixed values but swapped
|
2024-08-25 13:01:58 +02:00
|
|
|
expr = :(5.0 - 3.0)
|
|
|
|
testHelper(expr, var, param, -expectedResult)
|
|
|
|
|
|
|
|
# test with variables
|
|
|
|
expr = :(x1 - x2)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
# test with variables but swapped
|
|
|
|
expr = :(x2 - x1)
|
|
|
|
testHelper(expr, var, param, -expectedResult)
|
|
|
|
|
|
|
|
# test with parameters
|
|
|
|
expr = :(p1 - p2)
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
expr = :(p2 - p1)
|
|
|
|
testHelper(expr, var, param, -expectedResult)
|
2024-08-18 11:55:41 +02:00
|
|
|
end
|
2024-08-25 13:01:58 +02:00
|
|
|
|
|
|
|
@testset "Test single value operator interpretation" begin
|
2024-11-01 11:23:58 +01:00
|
|
|
var = Matrix{Float32}(undef, 1, 1)
|
|
|
|
param = Vector{Vector{Float32}}(undef, 1)
|
2024-09-01 12:09:29 +02:00
|
|
|
expectedResult = 3.0 # Not using "eval" because the variables are not stored in global scope
|
2024-08-25 13:01:58 +02:00
|
|
|
|
|
|
|
var[1,1] = -3.0
|
|
|
|
param[1] = [-3.0]
|
|
|
|
|
|
|
|
# test with fixed value
|
|
|
|
expr = :(abs(-3.0))
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
# test with variable
|
|
|
|
expr = :(abs(x1))
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
|
|
|
# test with parameter
|
|
|
|
expr = :(abs(p1))
|
|
|
|
testHelper(expr, var, param, expectedResult)
|
2024-08-18 11:55:41 +02:00
|
|
|
end
|
|
|
|
|
2024-09-01 12:09:29 +02:00
|
|
|
@testset "Test complex expressions" begin
|
2024-11-01 11:23:58 +01:00
|
|
|
var = Matrix{Float32}(undef, 2, 2)
|
|
|
|
param = Vector{Vector{Float32}}(undef, 2)
|
2024-09-01 12:09:29 +02:00
|
|
|
|
|
|
|
# var set 1
|
|
|
|
var[1,1] = 3.0
|
|
|
|
var[2,1] = 5.0
|
|
|
|
# var set 2
|
|
|
|
var[1,2] = 3.0
|
|
|
|
var[2,2] = -5.0
|
|
|
|
|
|
|
|
param[1] = [3.0]
|
|
|
|
param[2] = [5.0, 2.0]
|
|
|
|
|
|
|
|
expr1 = :((x1 + 5) * p1 - 3 / abs(x2) + (2^4) - log(8))
|
|
|
|
expr2 = :(1 + 5 * x1 - 10^2 + (p1 - p2) / 9 + exp(x2))
|
|
|
|
|
|
|
|
postfix = Vector([expr_to_postfix(expr1), expr_to_postfix(expr2)])
|
|
|
|
result = Interpreter.interpret(postfix, var, param)
|
|
|
|
|
|
|
|
# var set 1
|
|
|
|
@test isapprox(result[1,1], 37.32, atol=0.01) # expr1
|
2024-09-08 11:52:10 +02:00
|
|
|
@test isapprox(result[1,2], 64.74, atol=0.01) # expr2
|
2024-09-01 12:09:29 +02:00
|
|
|
# var set 2
|
2024-09-08 11:52:10 +02:00
|
|
|
@test isapprox(result[2,1], 37.32, atol=0.01) # expr1
|
|
|
|
@test isapprox(result[2,2], -83.65, atol=0.01) # expr2
|
2024-09-01 12:09:29 +02:00
|
|
|
end
|