Division between complex number to himself - math

Refers to WolframAlpha and some other calculators:
1i / 1i = -1
i / i = 1
My questions are:
Why division between complex number to himself does'nt return the value 1?
The both expressions should be the same, so why the results are diffrent?

You need to be aware of what the first expression means. In particular, where are the hidden parentheses.
1i/1i is in fact 1 * i / 1 * i, which is (((1 * i) / 1) * i) when all the parentheses are added.
Then, step by step, you get ((i / 1) * i), then (i * i), which is -1.
This is very different from (1i)/(1i), which is effectively 1.

Related

simplify algebraic expressions in R

I would like to solve a first order condition given an expression in R. I have successfully taken the derivative but, I suspect because I am taking the derivative of a bunch of call objects. I cannot get my expressions to simplify.
In the example code below I outline three functions, combine them with arithmetic and then take the derivative with respect to my K variable. The associated output has also been placed below.
Q = quote(K^(a)*L^(1-a))
P = quote(wL+x)
MC = quote(wL+K)
eval(parse(text=R-MC))
profit = substitute(Q*(P-MC), list(Q=Q,P=P,MC=MC))
D(profit,'K')
Output:
K^((a) - 1) * (a) * L^(1 - a) * (wL + x - (wL + K)) - K^(a) *
L^(1 - a)
Note that the (wL + x - (wL + K)) in the above output ought to simplify to (x - K). While the output is correct, I am afraid that if I went much further or tried to solve for a particular first order condition, I want to be working with the most simplified expressions possible
My question: Is there a function or means by which I can simplify expressions either prior to or once they have been mathematically evaluated?
My desired output (or any algebraic equivalent):
K^((a) - 1) * (a) * L^(1 - a) * (x - K) - K^(a) *
L^(1 - a)
Using the Ryacas0 package (also see the Ryacas package) we can write:
library(Ryacas0)
der <- D(profit,'K')
e <- as.expression(der)
Simplify(e)
giving:
yacas_expression(K^(a - 1) * a * L^(1 - a) * x - K^(a - 1) * a * L^(1 - a) * K - L^(1 - a) * K^a)

Time complexicity of recursive function

I have a recursive function f(n) with time complexicity
O(f(n)) = O(combination(n, n/2) * f(n/2)^2)
where combination(a, b) means combination nuber a above b.
I tried to simplify it, but don't have enough mathematical skills. The only thing that I foud out is that
combination(n,n/2) = 2^n * (gamma(n/2 + 1/2)/(sqrt(1/2) * gamma(n/2 + 1)))
I don't have experience with calculation of complexities but this seems to me to be of order n!. At least for calculation of special case with n=2^i. With integers combination(n, n/2) is equal to n!/((n/2)!)^2.
f(2^i) = (2^i)! / ((2^(i-1))!)^2 * f(2^(i-1))^2
= (2^i)! / ((2^(i-1))!)^2 * (2^(i-1))! / ((2^(i-2))!)^2)^2 * f(2^(i-2))^4
= (2^i)! / ((2^(i-2))!)^4 * f(2^(i-2))^4
...
= (2^i)! / (1!)^(2^i) = n!
I have allready solved it in this thread on satck exchange:
https://math.stackexchange.com/questions/2642848/time-complexicity-of-recursive-function?noredirect=1#comment5458208_2642848

How to find where an equation equals zero

Say I have a function and I find the second derivative like so:
xyr <- D(expression(14252/(1+exp((-1/274.5315)*(x-893)))), 'x')
D2 <- D(xyr, 'x')
it gives me back as, typeof 'language':
-(14252 * (exp((-1/274.5315) * (x - 893)) * (-1/274.5315) * (-1/274.5315))/(1 +
exp((-1/274.5315) * (x - 893)))^2 - 14252 * (exp((-1/274.5315) *
(x - 893)) * (-1/274.5315)) * (2 * (exp((-1/274.5315) * (x -
893)) * (-1/274.5315) * (1 + exp((-1/274.5315) * (x - 893)))))/((1 +
exp((-1/274.5315) * (x - 893)))^2)^2)
how do I find where this is equal to 0?
A little bit clumsy to use a graph/solver for this, since your initial function as the form:
f(x) = c / ( 1 + exp(ax+b) )
You derive twice and solve for f''(x) = 0 :
f''(x) = c * a^2 * exp(ax+b) * (1+exp(ax+b)) * [-1 + exp(ax+b)] / ((1+exp(ax+b))^3)
Which is equivalent that the numerator equals 0 - since a, c, exp() and 1+exp() are always positive the only term which can be equal to zero is:
exp(ax+b) - 1 = 0
So:
x = -b/a
Here a =-1/274.5315, b=a*(-893). So x=893.
Just maths ;)
++:
from applied mathematician point of view, it's always better to have closed form/semi-closed form solution than using solver or optimization. You gain in speed and in accuracy.
from pur mathematician point of view, it's more elegant!
You can use uniroot after having created a function from your derivative expression:
f = function(x) eval(D2)
uniroot(f,c(0,1000)) # The second argument is the interval over which you want to search roots.
#Result:
#$root
#[1] 893
#$f.root
#[1] -2.203307e-13
#$iter
#[1] 7
#$init.it
#[1] NA
#$estim.prec
#[1] 6.103516e-05

Rounding Twice - into one rule

I need to round a number by 1/32 and THEN round it by 1/100th. I need to convert this into one single rounding rule though (using an archaic program...). I can multiply and divide the original number and all that, just can't round twice....
Is there a way to do this mathematically?
Thanks!
kcross
If whatever you're using lets you define functions, the most readable implementation would be this:
function round(x, interval){
//implementation left as an exercise to the reader
}
#rounds x by interval1, then by interval2
function doubleRound(x, interval1, interval2){
return round(round(x, interval1), interval2)
}
but if all you have is simple arithmetic, you can unroll everything into one statement.
To round a non-negative number x to the nearest interval of N, you can use this formula:
round(x,N) = floor((x + (N/2)) / N) * N
to round twice, you nest the function within itself:
round(round(x, N1), N2) = floor(((floor((x + (N1/2)) / N1) * N1) + (N2/2)) / N2) * N2
so to round by 1/32 and then 1/100, you use:
floor(((floor((x + ((1/32)/2)) / (1/32)) * (1/32)) + ((1/100)/2)) / (1/100)) * (1/100)

Calculate bessel function in MATLAB using Jm+1=2mj(m) -j(m-1) formula

I tried to implement bessel function using that formula, this is the code:
function result=Bessel(num);
if num==0
result=bessel(0,1);
elseif num==1
result=bessel(1,1);
else
result=2*(num-1)*Bessel(num-1)-Bessel(num-2);
end;
But if I use MATLAB's bessel function to compare it with this one, I get too high different values.
For example if I type Bessel(20) it gives me 3.1689e+005 as result, if instead I type bessel(20,1) it gives me 3.8735e-025 , a totally different result.
such recurrence relations are nice in mathematics but numerically unstable when implementing algorithms using limited precision representations of floating-point numbers.
Consider the following comparison:
x = 0:20;
y1 = arrayfun(#(n)besselj(n,1), x); %# builtin function
y2 = arrayfun(#Bessel, x); %# your function
semilogy(x,y1, x,y2), grid on
legend('besselj','Bessel')
title('J_\nu(z)'), xlabel('\nu'), ylabel('log scale')
So you can see how the computed values start to differ significantly after 9.
According to MATLAB:
BESSELJ uses a MEX interface to a Fortran library by D. E. Amos.
and gives the following as references for their implementation:
D. E. Amos, "A subroutine package for Bessel functions of a complex
argument and nonnegative order", Sandia National Laboratory Report,
SAND85-1018, May, 1985.
D. E. Amos, "A portable package for Bessel functions of a complex
argument and nonnegative order", Trans. Math. Software, 1986.
The forward recurrence relation you are using is not stable. To see why, consider that the values of BesselJ(n,x) become smaller and smaller by about a factor 1/2n. You can see this by looking at the first term of the Taylor series for J.
So, what you're doing is subtracting a large number from a multiple of a somewhat smaller number to get an even smaller number. Numerically, that's not going to work well.
Look at it this way. We know the result is of the order of 10^-25. You start out with numbers that are of the order of 1. So in order to get even one accurate digit out of this, we have to know the first two numbers with at least 25 digits precision. We clearly don't, and the recurrence actually diverges.
Using the same recurrence relation to go backwards, from high orders to low orders, is stable. When you start with correct values for J(20,1) and J(19,1), you can calculate all orders down to 0 with full accuracy as well. Why does this work? Because now the numbers are getting larger in each step. You're subtracting a very small number from an exact multiple of a larger number to get an even larger number.
You can just modify the code below which is for the Spherical bessel function. It is well tested and works for all arguments and order range. I am sorry it is in C#
public static Complex bessel(int n, Complex z)
{
if (n == 0) return sin(z) / z;
if (n == 1) return sin(z) / (z * z) - cos(z) / z;
if (n <= System.Math.Abs(z.real))
{
Complex h0 = bessel(0, z);
Complex h1 = bessel(1, z);
Complex ret = 0;
for (int i = 2; i <= n; i++)
{
ret = (2 * i - 1) / z * h1 - h0;
h0 = h1;
h1 = ret;
if (double.IsInfinity(ret.real) || double.IsInfinity(ret.imag)) return double.PositiveInfinity;
}
return ret;
}
else
{
double u = 2.0 * abs(z.real) / (2 * n + 1);
double a = 0.1;
double b = 0.175;
int v = n - (int)System.Math.Ceiling((System.Math.Log(0.5e-16 * (a + b * u * (2 - System.Math.Pow(u, 2)) / (1 - System.Math.Pow(u, 2))), 2)));
Complex ret = 0;
while (v > n - 1)
{
ret = z / (2 * v + 1.0 - z * ret);
v = v - 1;
}
Complex jnM1 = ret;
while (v > 0)
{
ret = z / (2 * v + 1.0 - z * ret);
jnM1 = jnM1 * ret;
v = v - 1;
}
return jnM1 * sin(z) / z;
}
}

Resources