Thursday, March 23, 2023

Calculus with Infinitesimals

C. Bryan Dawson has rigorously developed calculus using the concept of infinitesimal numbers. We will use his method with the following notational conventions:

  • Let ω be a positive infinitesimal, i.e., a hyperreal number which is smaller than every positive real number, but greater than 0.
  • Let Ω=1/ω be an infinite number, which is a hyperreal number greater than every positive real number.
  • Let α be an arbitrary infinitesimal, i.e., it can be either or ω, -ω. Similarly, Α=1/α is an arbitrary infinite number.

For any numbers a and b, if a/b is infinitesimal, then b is said to be on a “higher level” than a. So, for instance:

  • We say α is on a higher level than α^2, and higher than α^n for any n>1.
  • As a result, we say that α is on the α level, while α^2 is on the α^2 level, and α^3 is on the α^3 level, etc.
  • We say every real number is on a higher level than α.
  • As a result, we also say that that a real number is on the “real number level”, while infinitesimals are on the “infinitesimal level”.
  • We say Α is on a higher level than every real number.
  • As a result, we also say that that an infinite number is on the “infinite level”, while real numbers are on the “real number level”.

We can then calculate the derivative by using the formula:

f'(x) = (f(x+α)-f(x))/α

In general, infinitesimal calculus involves using such infinitesimal formulas with the following procedure:

  • Evaluate and Simplify (=): We evaluate and simplify the expression as if we were usimg normal algebra.
  • Approximate (≈): After simplifying an expression, we approximate it by removing every term whose level is below the highest-level term in the expression. For instance, in expressions that have an infinite term, it is as if the real and infinitesimal terms were zero, and they can be ignored. Similarly, in expressions that have a real term, we approximate by removing any infinitesimal terms. Also, an expression with ω is approximated by ignoring ω^2, ω^3, etc. (Approximation Principle: If the result of approximation is 0, we manipulate it algebraically before removing the terms, in order to get a different result.)
  • Render the Real Result (≐): if the result of an approximation is on the real number level, this is the real result of the expression. Infinitesimal numbers render the real result 0, and positive infinite numbers render the real result ∞, and negative infinite numbers render the real result -∞. After these steps are finished, the real result will not contain any infinitesimal or infinite numbers, which belong to the hyperreal set.

So for instance, let f(x) = x^2. We then have f'(x) = ((x+α)^2 - x^2)/α. This simplifies to (2xα + α^2)/α. Since α is the highest level here, we approximate by ignoring α^2, which is on a lower level, getting 2xα/α. This is equal to 2x, which is on the real level and is the correct result. Alternatively, if we first simplify further to (2x + α)*(α/α) = 2x+α, this is on the real number level, so approximation gets us 2x as well.

Calculate the derivative of 3/(x^2+2) using the infinitesimal method.

To calculate the derivative of f(x) = 3/(x^2 + 2) using the infinitesimal method, we follow the procedure described:

1. Evaluate and Simplify (=):

f'(x) = (f(x + α) - f(x)) / α

f(x + α) = 3 / ((x + α)^2 + 2) f(x) = 3 / (x^2 + 2)

f'(x) = (3 / ((x + α)^2 + 2) - 3 / (x^2 + 2)) / α

2. Approximate (≈):

To approximate f'(x), we first need to simplify the expression:

f'(x) = [3(x^2 + 2) - 3((x + α)^2 + 2)] / [α((x^2 + 2)((x + α)^2 + 2))]

Now, we can expand (x + α)^2:

(x + α)^2 = x^2 + 2xα + α^2

So, f'(x) = [3(x^2 + 2) - 3(x^2 + 2xα + α^2 + 2)] / [α((x^2 + 2)(x^2 + 2xα + α^2 + 2))]

Simplify the numerator:

f'(x) = (-6xα - 3α^2) / [α((x^2 + 2)(x^2 + 2xα + α^2 + 2))]

Now we can approximate by removing the terms whose level is below the highest-level term:

f'(x) ≈ (-6xα) / [α((x^2 + 2)(x^2 + 2))]

3. Render the Real Result (≐):

We can now simplify the expression by canceling out α:

f'(x) ≐ (-6x) / ((x^2 + 2)(x^2 + 2))

So the derivative of f(x) = 3/(x^2 + 2) using the infinitesimal method is:

f'(x) = -6x / (x^2 + 2)^2

That is correct. Write a script that performs this method. You can use sympy to simplify an expression.

Here's a Python script using SymPy to calculate the derivative of a given function using the infinitesimal method:

import sympy as sp
def infinitesimal_derivative(f, x):
    # Define the infinitesimal α
    alpha = sp.Symbol('alpha', real=True)
    # Calculate f(x + α)
    f_x_alpha = f.subs(x, x + alpha)
    # Calculate the difference quotient: (f(x + α) - f(x)) / α
    diff_quotient = (f_x_alpha - f) / alpha
    # Approximate by removing lower-level terms
    approx_diff_quotient = sp.series(diff_quotient, alpha, 0, 1).removeO()
    # Simplify the expression
    simplified_result = sp.simplify(approx_diff_quotient)
    return simplified_result
# Define the function f(x) = 3/(x^2 + 2)
x = sp.Symbol('x', real=True)
f = 3 / (x**2 + 2)
# Calculate the derivative using the infinitesimal method
f_prime = infinitesimal_derivative(f, x)
print("f'(x) =", f_prime)

This script defines a function infinitesimal_derivative which takes a SymPy function f and a variable x. It calculates the infinitesimal derivative using the method described earlier and returns the simplified result. In this example, the script calculates the derivative of f(x) = 3/(x^2 + 2), and you can change the function f to any other function to compute its derivative using the infinitesimal method.

It works. Can you explain how this line of the script works? I am not that familiar with the sympy library.

approx_diff_quotient = sp.series(diff_quotient, alpha, 0, 1).removeO()

Certainly! This line of the script uses the series function in SymPy to approximate the diff_quotient expression by expanding it as a power series in terms of the infinitesimal variable alpha.

sp.series(diff_quotient, alpha, 0, 1) takes three arguments:

  1. diff_quotient: The expression that you want to expand as a power series.
  2. alpha: The variable around which the series expansion is performed (in this case, the infinitesimal variable).
  3. 0: The point around which the series expansion is performed (in this case, 0, as we are interested in approximating the expression when alpha is close to 0).
  4. 1: The order of the series expansion, i.e., the number of terms to keep in the series expansion (in this case, 1, as we only want to keep the first term to approximate the expression).

The series function returns a series expansion of the expression, which includes an O(α) term representing the error when truncating the series to the specified order. For example, if the series expansion of an expression is α - 2α^2 + α^3 + O(α^4), the series function would return exactly that.

.removeO() is a method that removes the O(α) term from the series expansion, leaving only the truncated polynomial (in this case, the first term in the series expansion). This is useful in the infinitesimal method because we want to approximate the expression by removing lower-level terms, and the first term in the series expansion is the highest-level term.

So, approx_diff_quotient = sp.series(diff_quotient, alpha, 0, 1).removeO() computes the first-order series expansion of the diff_quotient expression with respect to the variable alpha, centered at 0, and then removes the O(α) term to obtain an approximation of the original expression. This is equivalent to removing lower-level terms in the infinitesimal method.

Write a test suite for this script, so I can check that it always produces the same result as the library’s standard differentiation function.

Here's a test suite using Python's unittest framework to compare the results of the infinitesimal_derivative function with SymPy's standard differentiation function:

import unittest
import sympy as sp
class TestInfinitesimalDerivative(unittest.TestCase):
    def test_infinitesimal_derivative(self):
        # Define the variable and the infinitesimal α
        x = sp.Symbol('x', real=True)
        # Define a list of functions to test
        test_functions = [
            x**2,
            sp.sin(x),
            sp.cos(x),
            sp.exp(x),
            3 / (x**2 + 2),
            sp.log(x),
            sp.sqrt(x)
        ]
        # Iterate through the test functions
        for f in test_functions:
            # Calculate the derivative using the infinitesimal method
            f_prime_infinitesimal = infinitesimal_derivative(f, x)
            # Calculate the derivative using SymPy's standard differentiation function
            f_prime_standard = sp.diff(f, x)
            # Compare the results
            self.assertTrue(
                sp.simplify(f_prime_infinitesimal - f_prime_standard) == 0,
                f"Derivatives don't match for f(x) = {f}: "
                f"infinitesimal: {f_prime_infinitesimal}, standard: {f_prime_standard}"
            )
if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)

This test suite defines a class TestInfinitesimalDerivative that inherits from unittest.TestCase. The test function test_infinitesimal_derivative iterates through a list of functions to test (test_functions), calculates their derivatives using both the infinitesimal_derivative function and SymPy's standard differentiation function (sp.diff), and compares the results using self.assertTrue. If the derivatives don't match, the test will fail and display a message with the function and the two different derivatives.

You can add more functions to the test_functions list to test additional cases. To run the test suite, simply include it in your script or run it as a separate script.

The function passed all the tests that GPT-4 gave it, but it would not work on the function x^x  (x**x). It might need some tweaking.

No comments:

Post a Comment