sympy / sympy

A computer algebra system written in pure Python

Home Page:https://sympy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug with O(f) if f is a Function

jbarber-phys opened this issue · comments

The order function sympy.O(f) breaks if the type of f is a Function (but works fine for symbols); it throws out all terms, even lower order ones.
Example:

import sympy as sym
x = sym.symbols("x")
print((x+x**2+x**3+sym.O(x**3)))#will print: x + x**2 + O(x**3)
f = sym.Function("f")(x)
print((f+f**2+f**3+sym.O(f**3)))#will print: O(f(x)**3)

This was on Windows 11 python 3.12.1 (x64), sympy 1.12 .

The use of free symbols here means that variables is x rather than f(x):

variables = list(expr.free_symbols)

It seems that Order thinks that O(f(x)**3) should be understood as a function of x rather than a function of f(x).

I see. Unfortunately though, sym.O is not setup to accept functions as free symbols.
I have found a workaround involving replacing the function with a dummy symbol (and undoing this after calling O and removeO ).
Thank you.