Opened 10 months ago

Last modified 3 months ago

#31316 new defect

Method subs() ineffective on tensor fields

Reported by: egourgoulhon Owned by:
Priority: major Milestone: sage-9.5
Component: manifolds Keywords: tensor field, subs, substitution
Cc: tscrim, gh-mjungmath Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

As reported in this ask.sagemath question, tensor fields are endowed with a method subs() which is totally ineffective:

sage: E.<x,y> = EuclideanSpace()                                                                    
sage: a = var('a')                                                                                  
sage: v = E.vector_field(a*x, a^2 + y)                                                              
sage: v.display()                                                                                   
a*x e_x + (a^2 + y) e_y
sage: v.subs(a==1)                                                                                  
Vector field on the Euclidean plane E^2
sage: v.display()                                                                                   
a*x e_x + (a^2 + y) e_y

This is because subs() is inherited from the generic base class Element and the code of Element.subs() has

        parent = self._parent
        try:
            ngens = parent.ngens()
        except (AttributeError, NotImplementedError, TypeError):
            return self

Since v.parent().ngens() returns an AttributeError, this explains the silent failure.

Currently, the required substitution can be achieved by means of the method apply_map:

sage: v.apply_map(lambda comp: comp.subs(a==1))                                                     
sage: v.display()                                                                                   
x e_x + (y + 1) e_y

Change History (8)

comment:1 Changed 10 months ago by egourgoulhon

  • Cc tscrim gh-mjungmath added

comment:2 follow-up: Changed 10 months ago by gh-mjungmath

We could simply overload subs and delegate this to apply_map? Or is there a more effective way?

comment:3 in reply to: ↑ 2 Changed 10 months ago by egourgoulhon

Replying to gh-mjungmath:

We could simply overload subs and delegate this to apply_map?

Yes this is probably the route to go.

comment:4 Changed 10 months ago by egourgoulhon

Also, in another ticket, we could question the current treatment of AttributeError in Element.subs() as return self.

Last edited 10 months ago by egourgoulhon (previous) (diff)

comment:5 Changed 10 months ago by tscrim

Note that I would not expect v.subs(a==1) to do a substitution because it is passing in an expression a == 1, rather than setting a keyword a=1. Count the number of equals.

comment:6 Changed 10 months ago by gh-DaveWitteMorris

The subs method of elements of SR does allow (and document) this syntax, so it would make sense to allow it here, too.

sage: var('t');
sage: cos(x).subs(x==sin(t)
cos(sin(t))
Version 0, edited 10 months ago by gh-DaveWitteMorris (next)

comment:7 Changed 7 months ago by mkoeppe

  • Milestone changed from sage-9.3 to sage-9.4

Moving to 9.4, as 9.3 has been released.

comment:8 Changed 3 months ago by mkoeppe

  • Milestone changed from sage-9.4 to sage-9.5
Note: See TracTickets for help on using tickets.