Sets and solution sets
Nerdamer has two kinds of set:
- a set (
ValuesSet) of known values, written{a, b, c}; - a solution set (
SolutionSet), returned bysolve. Each entry is one solution, a whole family of solutions, or "every value".
Neither is a general set engine: there's no set-builder notation, no intervals as sets, and no infinite sets other than solution families.
Sets
Curly braces make a set. Each value appears once:
{a, b, a} {a, b}
{[1,2],[1,2]} {[1, 2]}
Two values are the same if Nerdamer can show they are equal; if it can't, both are kept. The order you write them in is kept for display, but it doesn't matter for equality.
Set operations
Three functions combine sets:
| Notation | Gives |
|---|---|
union(A, B, ...) | the values in any of the sets |
intersection(A, B, ...) | the values in all of them |
difference(A, B) | the values in A that aren't in B |
union({a,b},{b,c}) {a, b, c}
intersection({a,b},{b,c}) {b}
difference({a,b},{b,c}) {a}
To add a value to a set, take the union with a one-element set. This collects the distinct values of a list:
block(s:{}, each([1,2,1], x, s:union(s,{x})), s) {1, 2}
These compare sets:
| Notation | Means |
|---|---|
A <= B | A is a subset of B |
A < B | A is a proper subset of B |
A >= B | A is a superset of B |
A > B | A is a proper superset of B |
A == B | A and B have the same members |
{a,b} <= {a,b,c} 1
{a,b} == {b,a} 1
Two functions from Nerdamer 1.x ask the same kind of question, and also give 1 or 0:
| Notation | Gives 1 when |
|---|---|
intersects(A, B) | A and B have a value in common |
issubset(A, B) | every value of A is in B, the same as A <= B |
intersects({a,b},{b,c}) 1
intersects({a},{c}) 0
issubset({a},{a,b}) 1
issubset({a,b},{a}) 0
The 1.x spelling is_subset still works, but it's deprecated and records an api.deprecatedFunction warning. Use issubset.
In JavaScript, nerdamer.union('{a,b}', '{b,c}') takes strings, as in Notation, and so do nerdamer.intersects and nerdamer.issubset. union, intersection, difference, intersects and isSubset can also be imported from nerdamer/structures, and take sets. The ValuesSet methods do the same, and add symmetricDifference and isDisjointFrom. Like the functions, they return new sets and leave the originals unchanged:
import { Expression } from 'nerdamer/core';
import { ValuesSet, union } from 'nerdamer/structures';
const a = new ValuesSet([Expression.create('a'), Expression.create('b')]);
const b = new ValuesSet([Expression.create('b'), Expression.create('c')]);
union(a, b); // {a, b, c}
a.union(b); // {a, b, c}
a.intersection(b); // {b}
a.difference(b); // {a}
a.symmetricDifference(b); // {a, c}
a.isDisjointFrom(b); // false
There are also isSubsetOf(), isProperSubsetOf(), isSupersetOf() and isProperSupersetOf().
Functions apply to each member
Functions such as expand, abs and evaluate apply to each member and return a set. Members that become equal merge:
expand({(x+1)^2, x^2+2*x+1}) {1+2*x+x^2}
abs({-1, 1, 2}) {1, 2}
No arithmetic on sets
{a, b} + {c, d} and {a, b} * 2 are errors. Pairing members up would depend on their order, which a set doesn't have. Use the set operations above.
Changing a set
add()andaddMany()skip values already present.delete()removes a value equal to the one you pass.each()maps every member, merging equal results.- Replacing a member by index removes it if the new value is already in the set.
clear()empties it.
A set keeps its own copies. Values you add are copied, and elements, at() and indexing return copies, so changing what you get back never changes the set.
Solution sets
solve returns a SolutionSet:
solve(x^2-4,x) {2, -2}
solve(sin(x),x) {_n*pi}
solve(x=x,x) {all}
- An entry with
_nis a family: one solution for every integer_n. allmeans every value is a solution.
count() counts entries, so {_n*pi} has a count of 1 even though it stands for infinitely many solutions.
Two properties describe the result:
| Property | Values | Meaning |
|---|---|---|
solutionForm | finite, parametric, all | Plain solutions, at least one _n family, or every value |
solutionsType | numeric, symbolic, mixed | Whether the solutions were found numerically, exactly, or both |
solutionsType says how a solution was found, not how it looks: an exact method can return 1. Symbolic coefficients such as a don't make a result parametric; only _n does.
Picking one member of a family
Give _n a value for one evaluation:
sol: solve(sin(x),x);
evaluate(sol,{_n=>0})
{0}
Assign with :. _n itself can't be assigned (_n:0 is an error), and sol = solve(sin(x),x) is an error too, because = makes an equation.
In JavaScript, substitute into an entry:
const solutions = nerdamer.solve('sin(x)', 'x');
solutions.elements[0].subst('_n', 2).text(); // 2*pi
Listing the members of a family
generate lists members, in one of two ways. Both include their bounds.
By value, with interval: every solution between two numbers.
nerdamer.solve('sin(x)', 'x').generate({ interval: [-10, 10] });
// {-3*pi, -2*pi, -pi, 0, pi, 2*pi, 3*pi}
In Notation: realsolutions(solve(sin(x),x),-10,10).
By index, with indices: the members for a range of _n. These bounds are values of _n, not of the solutions.
const families = nerdamer.solve('sin(x)', 'x'); // {_n*pi}
const roots = families.generate({ indices: [-2, 2] });
roots.text(); // {-2*pi, -pi, 0, pi, 2*pi}
roots.count(); // 5
roots.solutionForm; // 'finite'
families.solutionForm; // 'parametric': the original is unchanged
Details:
- What each mode accepts.
indicesworks for any family with_n, periodic or not, and symbolic coefficients stay symbolic.intervalworks for families of the forma + b*_nwith real numbersaandb; other families throwUnsupportedOperationError. - Index values. They can be numbers, strings,
bigint, decimals, fractions or expressions, but must be exact integers. JavaScript numbers beyond the safe integer range are rejected; usebigintfor large indices. - Interval bounds. They must be finite real numbers in ascending order.
- Plain solutions. On a result without families,
indicesreturns a copy andintervalkeeps the real solutions in range. Complex solutions are dropped:realsolutions(solve(x^6-1,x),-10,10)gives{-1, 1}. - Every value.
allcan't be listed, and throws an error. - Undefined members. Members that are undefined or infinite are left out.
- Excluded families. If the solution set excludes a whole family of values, listing it throws
UnsupportedOperationErrorrather than risk a wrong answer. - The 1,000-member limit.
generatelooks at no more than 1,000 candidates per call. Past that, it setspartialtotrueon the result and records asolve.generationLimitwarning. The result then may be missing members in the range.
Real values from a system
realsolutions with four arguments takes the result of solveeqs and returns the distinct real values of one variable within an interval:
realsolutions(solveeqs([x+y=3,x-y=1]),x,1,3) {2}
realsolutions(solveeqs([x+y=3,x-y=1]),y,1,3) {1}
It returns only that variable's values, not the full solutions. It filters what solveeqs found; it doesn't search for more solutions. The interval includes its bounds and must be finite and ascending.
Both forms of realsolutions throw an error for input they can't handle, such as a number instead of a solution set, rather than returning the call unevaluated.
Other things a solution set records
partial: whether the result may be incomplete.getExcluded(): values that were rejected while checking the solutions, such as points where a denominator is zero. An excluded value stays out even if it is added again later.getRawRoots(): details of the numerical root finding.
copy(), getExcluded() and getRawRoots() return independent copies. append() merges another solution set into this one, combining exclusions, partial state and solution types.
