Skip to content

Base Operator Overloading - Question #121

@maxreiss123

Description

@maxreiss123

Hi,

Thanks for such an incredible package; I am currently experimenting with the vector capabilities. According to the example you gave, is there a reason for not overloading the base operators, e.g. doing some like:

import Base: +, *
using DynamicExpressions
using DynamicExpressions: @declare_expression_operator


T = Union{Float64,Vector{Float64}}

+(x::AbstractVector, y::AbstractVector) = x .+ y
+(x::AbstractFloat, y::AbstractVector) = y .+ x
+(x::AbstractVector, y::AbstractFloat) = x .+ y

*(x::AbstractVector, y::AbstractVector) = x .* y


@declare_expression_operator(+, 2)
@declare_expression_operator(*, 2)

operators = GenericOperatorEnum(
    binary_operators=[+, *])

variable_names = ["x1"]


c1 = Expression(Node{T}(val=0.0); operators, variable_names)
c2 = Expression(Node{T}(val=[1.0, 2.0, 3.0]); operators, variable_names)
x1 = Expression(Node{T}(feature=1); operators, variable_names )

expression = ((x1 * x1) + c2) + c1

X = [[-1.0, 5.2, 0.1], [0.0, 0.0, 0.0]]

result = expression(X)
println(result)

Best wishes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions