文章目录
- [Technical Report: Symbolic Domain and Range Analysis of Real-Valued Functions Using SymPy](#Technical Report: Symbolic Domain and Range Analysis of Real-Valued Functions Using SymPy)
-
- [1. Introduction](#1. Introduction)
- [2. Methodology](#2. Methodology)
-
- [2.1 Domain Computation](#2.1 Domain Computation)
- [2.2 Range Computation](#2.2 Range Computation)
- [3. Implementation](#3. Implementation)
- [4. Test Cases and Results](#4. Test Cases and Results)
-
- [4.1 Case 1: f ( x ) = x x − 1 + ln ( 2 − x ) f(x) = \frac{\sqrt{x}}{x-1} + \ln(2-x) f(x)=x−1x +ln(2−x)](#4.1 Case 1: f ( x ) = x x − 1 + ln ( 2 − x ) f(x) = \frac{\sqrt{x}}{x-1} + \ln(2-x) f(x)=x−1x +ln(2−x))
- [4.2 Case 2: f(x) = \\sqrt{x\^2 - 5x}](#4.2 Case 2: f(x) = \sqrt{x^2 - 5x})
- [4.3 Case 3: f ( x ) = arcsin ( x ) f(x) = \arcsin(x) f(x)=arcsin(x)](#4.3 Case 3: f ( x ) = arcsin ( x ) f(x) = \arcsin(x) f(x)=arcsin(x))
- [5. Discussion](#5. Discussion)
- [6. Conclusion](#6. Conclusion)
- [7. Source](#7. Source)
Technical Report: Symbolic Domain and Range Analysis of Real-Valued Functions Using SymPy
1. Introduction
In mathematical analysis and engineering applications, determining the domain and range of a function is a fundamental step. While numeric sampling can provide approximate results, symbolic computation yields exact mathematical descriptions in terms of intervals and unions, which are essential for rigorous reasoning.
This report presents a Python-based tool using the SymPy library to automatically compute the domain (as the set of real numbers where the function is defined) and the range (the set of all possible output values) of a given elementary function. The approach relies on symbolic exclusion of "illegal" operations (division by zero, negative radicands, non-positive logarithm arguments, etc.) and solving equations for the range.
2. Methodology
2.1 Domain Computation
The domain is obtained using continuous_domain(function, variable, domain). This function analyzes the expression and returns the largest subset of the specified domain (here R \mathbb{R} R) on which the function is continuous and defined. It automatically handles:
- Denominators: exclude points where they vanish.
- Even-indexed radicals: require the radicand to be non-negative.
- Logarithms: require the argument to be strictly positive.
- Inverse trigonometric functions (e.g., arcsin \arcsin arcsin, arccos \arccos arccos): restrict the argument to − 1 , 1 -1,1 −1,1.
The result is returned as a set of intervals (closed, open, or half-open) and their unions.
2.2 Range Computation
The range is computed using function_range(function, variable, domain). This function finds the set of all y y y such that the equation f ( x ) = y f(x) = y f(x)=y has at least one solution x x x in the given domain. It internally solves for y y y using symbolic equation solving and interval analysis.
For functions that are continuous on their domain, the range is determined by evaluating limits at endpoints and critical points (where the derivative is zero). The result is also expressed as intervals or unions.
3. Implementation
The Python script is concise and flexible. The user only needs to define the function expression; the script then prints the domain and range in a human-readable format.
python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sympy import symbols, S, sqrt, log, asin, exp, sin, cos, tan
from sympy.calculus.util import continuous_domain, function_range
# Define the independent variable
x = symbols('x', real=True)
# Define the function to analyze (modify this line as needed)
f = sqrt(x) / (x - 1) + log(2 - x) # Example 1
# f = sqrt(x**2 - 5*x) # Example 2
# f = asin(x) # Example 3
print("=" * 60)
print(f"Analyzing function: f(x) = {f}")
print("=" * 60)
# Compute the domain
try:
domain = continuous_domain(f, x, S.Reals)
print(f"\nDomain:\n{domain}")
except Exception as e:
print(f"\nCould not compute domain: {e}")
domain = None
# Compute the range (requires the domain)
if domain is not None:
try:
range_y = function_range(f, x, domain)
print(f"\nRange:\n{range_y}")
except Exception as e:
print(f"\nCould not compute range directly: {e}")
print("Manual analysis may be required.")
else:
print("\nRange cannot be determined without domain.")
print("\n" + "=" * 60)
print("Notation:")
print(" - Interval.open(a, b) : open interval (a, b)")
print(" - Interval(a, b) : closed interval [a, b]")
print(" - Interval.Ropen(a, b) : left-closed, right-open [a, b)")
print(" - Interval.Lopen(a, b) : left-open, right-closed (a, b]")
print(" - Union(...) : set union")
print(" - oo : infinity")
print("=" * 60)
4. Test Cases and Results
We applied the script to three representative functions.
4.1 Case 1: f ( x ) = x x − 1 + ln ( 2 − x ) f(x) = \frac{\sqrt{x}}{x-1} + \ln(2-x) f(x)=x−1x +ln(2−x)
Mathematical analysis:
- Domain: x ≥ 0 x \ge 0 x≥0, x ≠ 1 x \neq 1 x=1, and x < 2 x < 2 x<2 → [ 0 , 1 ) ∪ ( 1 , 2 ) [0,1) \cup (1,2) [0,1)∪(1,2).
- Range: as x → 1 + x \to 1^+ x→1+, the term x / ( x − 1 ) → + ∞ \sqrt{x}/(x-1) \to +\infty x /(x−1)→+∞; as x → 1 − x \to 1^- x→1−, it tends to − ∞ -\infty −∞. The function is continuous on both subintervals, so it attains all real values → R \mathbb{R} R.
Program output:
Domain:
Union(Interval.Ropen(0, 1), Interval.open(1, 2))
Range:
Interval(-oo, oo)
4.2 Case 2: f(x) = \\sqrt{x\^2 - 5x}
Mathematical analysis:
- Domain: x 2 − 5 x ≥ 0 x^2 - 5x \ge 0 x2−5x≥0 → x \\le 0 or x \\ge 5 → ( − ∞ , 0 ] ∪ 5 , ∞ ) (-\\infty,0 \cup 5,\\infty) (−∞,0∪[5,∞).
- Range: the radicand can take any non-negative value, and the square root yields all y ≥ 0 y \ge 0 y≥0 → [ 0 , ∞ ) [0, \infty) [0,∞).
Program output:
Domain:
Union(Interval(-oo, 0), Interval(5, oo))
Range:
Interval(0, oo)
4.3 Case 3: f ( x ) = arcsin ( x ) f(x) = \arcsin(x) f(x)=arcsin(x)
Mathematical analysis:
- Domain: x ∈ − 1 , 1 x \in -1,1 x∈−1,1.
- Range: y ∈ − π / 2 , π / 2 y \in -\\pi/2, \\pi/2 y∈−π/2,π/2.
Program output (expected):
Domain:
Interval(-1, 1)
Range:
Interval(-pi/2, pi/2)
5. Discussion
The tool provides exact, closed-form interval descriptions for a wide class of elementary functions. It effectively implements the "exclude illegal regions" approach:
- For the domain, it identifies and removes points where the function is undefined.
- For the range, it symbolically solves for y y y and finds the minimum and maximum over the domain, including limits at boundaries.
Limitations:
- For highly transcendental or piecewise-defined functions,
function_rangemay fail; in such cases, manual analysis or numerical methods may be needed. - The computation relies on SymPy's internal algorithms, which are continuously improved.
Nevertheless, for typical functions encountered in calculus and engineering, the script yields reliable and exact results with minimal user effort.
6. Conclusion
We have developed a compact Python script that leverages SymPy to compute the domain and range of real-valued functions in a mathematically exact manner. The tool is easy to use, outputs standard interval notation, and has been validated on multiple test cases. It can be seamlessly integrated into educational environments, automated reasoning pipelines, or engineering design workflows where symbolic knowledge of function behavior is required.
7. Source
bash
PS E:\ANNA>python.exe e:/ANNA/experiments/get_function_range.py
============================================================
分析函数: f(x) = sqrt(x)/(x - 1) + log(2 - x)
============================================================
【定义域】
Union(Interval.Ropen(0, 1), Interval.open(1, 2))
【值域】
Interval(-oo, oo)
============================================================
说明:
- Interval.open(a, b) 表示开区间 (a, b)
- Interval(a, b) 表示闭区间 [a, b]
- Union(...) 表示并集
- oo 表示无穷大
============================================================
PS E:\ANNA>python.exe e:/ANNA/experiments/get_function_range.py
============================================================
分析函数: f(x) = sqrt(x**2 - 5*x)
============================================================
【定义域】
Union(Interval(-oo, 0), Interval(5, oo))
【值域】
Interval(0, oo)
============================================================
说明:
- Interval.open(a, b) 表示开区间 (a, b)
- Interval(a, b) 表示闭区间 [a, b]
- Union(...) 表示并集
- oo 表示无穷大
============================================================
PS E:\ANNA>