How to programmatically perform approximations on SageMath - symbolic-math

Starting with an example, consider the following expression
sage: EX=h/(h-z)
sage: EX
h
─────
h - z
Let's say I want to make the approximation h>>z. This would yield the result that EX ~ 1. So, how do I achieve this result on SageMath?
For now all I can do is just to stop, and manually rewrite something like
# If h>>z
sage: EX_approx=1
Is there any way that I can modify such expressions automatically/programmatically in the code? Keeping in mind that this simple example is just an example, and I wanna be able to do this for any equation.
I've tried things like
EX(h>>z)
EX.assume(h>>z)
limit(EX, (h-z)=h)
The thing that works for some cases is
limit(EX, z=0)
but that's strictly not the same thing and it doesn't work for cases like this:
sage: EX2=integrate(z^2*exp(EX), z)
sage: EX2
⌠
h ⎮ h
───── ⎮ ─────
⎛ 2 2 3⎞ h - z ⎮ ⎛ 4 3 ⎞ h - z
⎝3⋅h ⋅z + h⋅z - 2⋅z ⎠⋅ℯ ⎮ -⎝3⋅h - h ⋅z⎠⋅ℯ
- ───────────────────────────── - ⎮ ────────────────────── dz
6 ⎮ ⎛ 2 2⎞
⎮ 6⋅⎝h - 2⋅h⋅z + z ⎠
⌡
sage: limit(EX2, z=0)
1/2*h^3*e - 1/6*h^3*limit(integrate(z*e^(h/(h - z))/(h^2 - 2*h*z + z^2), z), z, 0)

An approximation to some order of a "small" parameter can be obtained with taylor method:
integrate(z^2*exp(EX).taylor(z, 0, 3), z)
returns
1/180*(60*h^3*z^3*e + 45*h^2*z^4*e + 54*h*z^5*e + 65*z^6*e)/h^3

Related

Texas Instrument CLA float inverse trick - What is its purpose

I have this piece of code written by somebody else that runs on a TI TMS320 Command Law Accelerator. So it's optimized in size and speed.
In order to get 1/x, the code always does something like this.
float32 y = __meinvf32(x);
y = y * (2.0f - y*x);
y = y * (2.0f - y*x);
I found this thread that propose something similar, but in my case, there is no clamping at the end.
Can seomebody help me understand what is the intent behind this?
Isaac Newton figured this out.
__meinvf32(x) gives an approximation of 1/x, say 1/x • (1+e), where e is some small relative error.
Let y = 1/x • (1+e). Then, when we calculate y • (2 − y•x), we have:
y • (2 − y•x) =
(1/x • (1+e)) • (2 − (1/x • (1+e))•x) =
1/x • (1+e) • (2 − (1+e)) =
1/x • (2 + 2e − (1+e) − e(1+e)) =
1/x • (2 + 2e − 1 − e − e − e2) =
1/x • (1 − e2).
Since e is small, e2 is even smaller. Thus, by calculating y • (2 − y•x) we get an estimate of 1/x that is closer than before; the relative error is only −e2 instead of e. Repeating this improves the estimate again (up to the limits of floating-point precision).
With some knowledge of bounds on the initial e, we can calculate how many repetitions are needed to get the estimate as close as desired to the correct result.
y = e + 1/x where e is some small error.
So (2.0 - y*x) is close to 1.0 and has the effect of reducing e on each pass.

Variable elimination in Bayes Net on a node with single child

If we have a node X, that has a child Y in a Bayes net, why is it correct to express P(Y) as P(Y|X)P(X)? Does it then follow that X is a necessary condition for Y?
Bayes Networks
An edge in a Bayes Network means the variable is conditionally dependent. If nodes are not connected by any path, they are conditionally independent.
Having a node X with a child Y means you need to learn:
Given X is True, what is the probability of Y being True?
Given X is False, what is the probability of Y being True?
More generally: If X can have n values and Y can have m values, then you have to learn n * (m - 1) values. The - 1 is there because the probabilities need to sum up to 1.
Example
Let's stick with the simple case that both variables are binary and use the following from Wikipedia:
Say X is RAIN and Y is SPRINKLER. You want to express Y (SPRINKLER) in terms of X (RAIN).
The Bayes theorem states:
P(Y|X) = P(X|Y) * P(Y) / P(X)
<=> P(Y) = P(Y | X) * P(X) / P(X | Y)
Now we apply the Law of total probability for X. This means, for X we simply go through all possible values:
P(Y) = P(Y | X = true) * P(X = true) +
P(Y | X = false) * P(X = false)
I guess this is what you refer to. P(X=true | Y) = 1, because the X=true means we already know that X=true happened. It doesn't matter what Y is.
To continue our case, we now look up the values in the tables (X is RAIN, Y is SPRINKLER):
P(Y) = 0.01 * 0.2 + 0.4 * 0.8
= 0.322

How to find all possible options in C?

I'm trying to find a efficient algorithm in C, which provides me all options of a given equation.
I have equation AX + BY = M, where A, B and M i got on input (scanf).
For example lets have: 5X + 10Y = 45
1st option: 5 * 9 + 10 * 0
2nd option: 5 * 7 + 10 * 1
n-th option: 5 * 1 +
10 * 4
And also I need to count how many possible options exist?
Some tips, hints?
I forgot to say that X and Y are in Z and >= 0, so there is no infinite options.
The question makes sense if you restrict to non-negative unknowns.
Rewrite the equation as
AX = M - BY.
There can be positive solutions as long as the RHS is positive, i.e.
BY ≤ M,
or
Y ≤ M/B.
Then for a given Y, there is a solution iff
A|(M - BY)
You can code this in Python as
for Y in range(M / B + 1):
if (M - B * Y) % A == 0:
X= (M - B * Y) / A
The solutions are
9 0
7 1
5 2
3 3
1 4
The number of iterations equals M / B. If A > B, it is better to swap X and Y.
you can calcule every solution if you put some limit in your input value, for example: use X and Y in a value included from 0 to 9... in this way you can use for to calculate every solution.
The number of solution is infinite:
find a first solution like: X=9, Y=0.
you can create another solution by using:
X' = X+2*p
Y' = Y-p
For any p in Z.
This proves your program will never terminate.

How to define some variables as non-commutative in Maxima

For example, I'd like to define x and y as non-commutative, and a and b as commutative (as usual). In other words,
x y ≠ y x, a x = x a, a b = b a .
Further,
(x + a y) (x - a y) = x^2 + a (y x - x y) - a^2 y^2 .
What is a code for defining x and y, and a symbol for multiplication (such as * and . ) ?
You can work with Maxima's commutative * and non-commutative . products in the way that you want by following the next two steps:
Declare the symbols a and b as scalars:
declare([a, b], scalar)$
Enable dotscrules:
dotscrules: true$
This simplifies non-commutative products involving scalars to commutative products (i.e., a.x becomes a*x).
Now you are ready. For example,
expand((a*x + b*y) . (a*x - b*y))
returns
a*b*y.x - b^2*y^^2 - a*b*x.y + a^2*x^^2
(note that ^^ is the non-commutative exponentiation operator).

FIND-S Algorithm - simple question

The FIND-S algorithm is probably one of the most simple machine learning algorithms. However, I can't find many examples out there.. Just the standard 'sunny, rainy, play-ball' examples that's always used in machine learning. Please could someone help me with this application (its a past exam question in machine learning).
Hypotheses are of the form a <= x <= b, c <= y <= d where x and y are points in an x,y plane and c and d are any integer. Basically, these hypotheses define rectangles in the x,y space.
These are the training examples where - is a negative example and + is a positive example and the pairs are the x,y co-ordinates:
+ 4, 4
+ 5, 3
+ 6, 5
- 1, 3
- 2, 6
- 5, 1
- 5, 8
- 9, 4
All I want to do is apply FIND-S to this example! It must be simple! Either some tips or a solution would be awesome.
Thank you.
Find-S seeks the most restrictive (ie most 'specific') hypothesis that fits all the positive examples (negatives are ignored).
In your case, there's an obvious graphical interpretation: "find the smallest rectangle that contains all the '+' coordinates"...
... which would be a=4, b=6, c=3, d=5.
The algorithm for doing it would be something like this:
Define a hypothesis rectangle h[a,b,c,d], and initialise it to [-,-,-,-]
for each + example e {
if e is not within h {
enlarge h to be just big enough to hold e (and all previous e's)
} else { do nothing: h already contains e }
}
If we step through this with your training set, we get:
0. h = [-,-,-,-] // initial value
1. h = [4,4,4,4] // (4,4) is not in h: change h so it just contains (4,4)
2. h = [4,5,3,4] // (5,3) is not in h, so enlarge h to fit (4,4) and (5,3)
3. h = [4,6,3,5] // (6,5) is not in h, so enlarge again
4. // no more positive examples left, so we're done.

Resources