Skip to content

Cannot add edge with Num type weights (from Symbolics) #49

Description

@kockahonza

Hi, so I'm trying to use the package for a graph with terms from Symbolics.jl as the weights. I'm pretty sure everything should work just fine except when I try to add a new edge via add_edge! which checks if the weight is zero and which does not work for these terms. For a MWE:

using Symbolics, SimpleWeightedGraphs, Graphs
g = SimpleWeightedDiGraph{Int,Num}(2)
add_edge!(g, 1, 2, 2.0) # works
@variables x
add_edge!(g, 1, 2, x) # does not work

This also seems like it could very well apply to other types, the problem here is the == operator does not do the same thing for Num. A simple fix may be to add this check in a try block and maybe print a warning that the check could not be done? Though such a warning should be toggleable to avoid excessive printing.

Activity

  1. jpfairbanks commented on Oct 11, 2024

    @jpfairbanks
    Member

    Symbolic.jl types require you to use isequal instead of ==. The isequal function should do the right thing for normal numeric types so we should use that to avoid this issue, rather than anything fancier. It's annoying that == builds equations terms for symbolic types, because it forces any Julia code that could get a symbolic type to use isequal, but it lets them write really clean looking equations in regular Julia syntax.

  2. kockahonza commented on Dec 9, 2024

    @kockahonza
    Author

    Hi, yes after getting more experience with Symbolics this is absolutely an annoying problem on their side more than here (pops up all the time for me). That said I have been using simply weighted graphs with Num edge types quite successfully over the past couple of months with just a simple fix/improvement. I have just created a pull request, it replaces the comparison to zero using == with a call to iszero which seems like an uncontroversial improvement. This is done when using add_edge! just to print a warning that adding edges of weight 0 does nothing. The pull request is here #50 and hopefully it gets approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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