Skip to content

Commit c1d4f3b

Browse files
Add comprehensive docstrings for core inspection functions
Adds detailed documentation for three fundamental inspection functions that are essential for working with symbolic expressions: - iscall(): Check if an expression represents a function call - operation(): Extract the function/operator from a call expression - arguments(): Get the arguments passed to an operation Features added: - Comprehensive explanations of each function's purpose and behavior - Practical examples showing real usage patterns with @variables - Clear guidance on when each function should be used - Cross-references between related functions - Examples covering arithmetic operations, function calls, and nested expressions These functions are frequently used in symbolic computation and were missing proper documentation, making them difficult to understand for new users. The docstrings follow Julia documentation conventions and include runnable examples to help users understand how to effectively traverse and analyze symbolic expression trees. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 6fbe6c9 commit c1d4f3b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/types.jl

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,40 @@ symtype(x) = typeof(x)
134134
@inline symtype(::Symbolic{T}) where T = T
135135
@inline symtype(::Type{<:Symbolic{T}}) where T = T
136136

137+
"""
138+
operation(expr)
139+
140+
Extract the operation (function) from a symbolic function call expression.
141+
Only valid for expressions where `iscall(expr)` returns `true`.
142+
143+
Returns the function/operator that is being applied in the expression. For basic
144+
arithmetic, this returns the operator function (+, -, *, /, ^). For function calls
145+
like `sin(x)`, this returns the function `sin`.
146+
147+
# Examples
148+
```julia
149+
using SymbolicUtils
150+
@variables x y
151+
152+
# Arithmetic operations
153+
expr1 = x + y
154+
operation(expr1) # returns +
155+
156+
expr2 = x * y
157+
operation(expr2) # returns *
158+
159+
# Function calls
160+
expr3 = sin(x)
161+
operation(expr3) # returns sin
162+
163+
# Nested expressions
164+
expr4 = sin(x + y)
165+
operation(expr4) # returns sin
166+
operation(arguments(expr4)[1]) # returns +
167+
```
168+
169+
See also: [`iscall`](@ref), [`arguments`](@ref)
170+
"""
137171
# We're returning a function pointer
138172
@inline function operation(x::BasicSymbolic)
139173
@compactified x::BasicSymbolic begin
@@ -200,6 +234,41 @@ end
200234

201235
TermInterface.children(x::BasicSymbolic) = arguments(x)
202236
TermInterface.sorted_children(x::BasicSymbolic) = sorted_arguments(x)
237+
238+
"""
239+
arguments(expr)
240+
241+
Extract the arguments from a symbolic function call expression.
242+
Only valid for expressions where `iscall(expr)` returns `true`.
243+
244+
Returns a collection (typically a vector) containing the arguments passed to the operation.
245+
For binary operations like `+` or `*`, this returns a collection of all operands.
246+
For function calls, this returns the function arguments.
247+
248+
# Examples
249+
```julia
250+
using SymbolicUtils
251+
@variables x y z
252+
253+
# Binary arithmetic operations
254+
expr1 = x + y
255+
arguments(expr1) # returns collection containing x and y
256+
257+
expr2 = x * y * z
258+
arguments(expr2) # returns collection containing x, y, and z
259+
260+
# Function calls
261+
expr3 = sin(x)
262+
arguments(expr3) # returns collection containing x
263+
264+
# Nested expressions
265+
expr4 = sin(x + y)
266+
arguments(expr4) # returns collection containing (x + y)
267+
arguments(arguments(expr4)[1]) # returns collection containing x and y
268+
```
269+
270+
See also: [`iscall`](@ref), [`operation`](@ref)
271+
"""
203272
function TermInterface.arguments(x::BasicSymbolic)
204273
@compactified x::BasicSymbolic begin
205274
Term => return x.arguments
@@ -249,6 +318,36 @@ function TermInterface.arguments(x::BasicSymbolic)
249318
end
250319

251320
isexpr(s::BasicSymbolic) = !issym(s)
321+
322+
"""
323+
iscall(expr)
324+
325+
Check if a symbolic expression `expr` represents a function call. Returns `true` if the
326+
expression is a composite expression with an operation and arguments, `false` otherwise.
327+
328+
This function is fundamental for traversing and analyzing symbolic expressions. In
329+
SymbolicUtils.jl, an expression is considered a "call" if it represents a function
330+
application (including operators like +, -, *, etc.).
331+
332+
# Examples
333+
```julia
334+
using SymbolicUtils
335+
@variables x y
336+
337+
# Basic variables are not calls
338+
iscall(x) # false
339+
340+
# Function calls are calls
341+
expr = sin(x + y)
342+
iscall(expr) # true
343+
344+
# Arithmetic expressions are calls
345+
iscall(x + y) # true
346+
iscall(x * y) # true
347+
```
348+
349+
See also: [`operation`](@ref), [`arguments`](@ref)
350+
"""
252351
iscall(s::BasicSymbolic) = isexpr(s)
253352

254353
@inline isa_SymType(T::Val{S}, x) where {S} = x isa BasicSymbolic ? Unityper.isa_type_fun(Val(SymbolicUtils.BasicSymbolic), T, x) : false

0 commit comments

Comments
 (0)