Assumptions

An assumption tells Nerdamer that a variable lies in a range of real numbers, such as x > 0. Comparisons and sign-dependent simplifications then use that range.

import nerdamer from 'nerdamer';
import { assume, clearAssumptions } from 'nerdamer/assumptions';

assume('x > 0');
nerdamer('simplify(log(x)+log(2))').text();   // log(2*x)
clearAssumptions();

Nerdamer concludes only what the range proves. If the range still allows more than one answer, the answer is "unknown", and nothing is assumed.

Writing assumptions

The examples below use these imports:

import nerdamer from 'nerdamer';
import { Expression } from 'nerdamer/core';
import {
    Assumption, assume, clearAssumptions, forgetAssumptionFor, getAssumptionFor,
} from 'nerdamer/assumptions';

In JavaScript

assume takes a variable, a comparison and a number:

assume('x > 0');
assume('n <= 12');
assume('rate >= -2.5');
assume('x', new Assumption(0, 10, true, false));   // 0 <= x < 10
  • Variable: a plain name, such as x or rate.
  • Comparison: =, ==, >, >=, < or <=.
  • Bound: a number, or inf, infinity, +inf, +infinity, -inf, -infinity. Simple arithmetic is calculated exactly: assume('y = (1+2)/6') gives exactly 1/2, and assume('z = 2^-3') exactly 1/8. Fractional powers, such as 2^(1/2), aren't accepted here; use Notation for those.

In Notation

assume(...) in an expression works the same, and also accepts exact irrational bounds and relations between two variables:

assume(x >= 0)
assume(x == 1/3)
assume(x > sqrt(2))
assume(x < y+1)
assume(2*x+3 < 2*y+5)

Bounds stay exact: 1/3 is not rounded, and sqrt(2) is compared exactly.

Relations between two variables

A relation such as assume(x<y+1) relates x to y by a constant. Nerdamer then works out a bound for x from whatever y's range is at the time:

nerdamer('assume(x<y+1)');
nerdamer('assume(y<=3)');
nerdamer('x').lt(4);   // true

The relation is kept even while y has no range. Only a constant difference works: products of variables, functions and other nonlinear relations, such as assume(x<y^2), are errors.

Combining and removing

Assumptions about the same variable combine:

assume('x >= 0');
assume('x < 10');
getAssumptionFor('x')?.toString();   // "[0, 10)"

An assumption that contradicts the current ones throws an error, and the earlier ones stay.

forgetAssumptionFor('x');   // remove x's assumptions, and relations that use x
clearAssumptions();         // remove all

In Notation, use forget(x) and forget(all).

getAssumptionFor(x) returns only the range you stated for x, not one derived from a relation.

What can't be assumed

  • Constants and the imaginary unit: assume(pi>0) is an error.
  • Properties such as integer(x), real(x) or even(x).
  • A union of ranges, such as x in [0, 1] or [2, 3].

What Nerdamer can prove

With assumptions, Nerdamer can prove the sign of:

ExpressionAssumptionsProves
A sum of terms with known signsa>0, b>0a+b>0
A linear combinationa>5, b<3a-b-2>0
Products and powers of positive valuesa>1, x>2, b<3a*x^2-b>0
An even powerx<-2, b<3x^2-b>0
An even power plus a positive number-1<x<1x^2+1>0
A product with a nonnegative factorx>=0, y>0x*y>=0 (but not x*y>0)
A reciprocalx>21/2-1/x>0
A square rootx>4sqrt(x)-2>0
A square root, from above0<=x<93-sqrt(x)>0
A comparison with an exact irrational boundx>sqrt(2)x>1.41

Some limits:

  • An even power is never negative, but that doesn't make it positive: with -1<x<1, x^2>0 isn't proved, because x can be 0.
  • A strict result needs strict bounds: if a bound allows the expression to be 0, >0 isn't proved.
  • Reciprocal bounds need a range that excludes zero, and square-root bounds need a range of nonnegative values.
  • Sign-changing bases, general nonlinear functions, and comparisons against arbitrary expressions may stay unknown.

Assumptions are used by comparisons (==, <, > and the rest), by functions that depend on sign such as abs, sqrt, max, min and sign, and by simplifications such as combining logarithms.

True, false or unknown

An Assumption compares in three ways: true if every value in the range satisfies the comparison, false if none does, and undefined if some do and some don't.

const x = new Assumption(0, 9);   // 0 <= x <= 9

x.gte(Expression.create(0));   // true
x.lt(Expression.create(10));   // true
x.gt(Expression.create(5));    // undefined
x.eq(Expression.create(5));    // undefined
x.eq(Expression.create(20));   // false

The comparison methods on expressions, eq(), gt(), gte(), lt() and lte(), return only true or false, and false also when the answer is unknown. To tell "disproved" from "not proved", ask the Assumption, from getAssumptionFor, directly.

Two variables with the same range aren't equal:

assume('x >= 0'); assume('x <= 9');
assume('y >= 0'); assume('y <= 9');

nerdamer('x').eq('y');   // false: not proved

To ask whether two ranges are the same, use Assumption.sameInterval().

Assumptions apply to your whole app

Assumptions stay in effect, for every later calculation, until you remove them. There's no way yet to limit them to one calculation, so clear them between independent calculations, and don't run calculations that need different assumptions side by side.