How to solve an equation system including log function in Mathematica - wolfram-mathematica

I am a beginner to Mathematica. I am trying to get the solution of such equation as:
Solve[-b LMR + LMA RGR - a LMR log10[e LMA + (d LMA (1 - LMR) SAR)/RGR] == 0, RGR].
Constants are a,b,c,d,e,LMR,LMA, and SAR.
But there seems some error as
'Solve::nsmet: This system cannot be solved with the methods available to Solve.'
Who knows how to solve this equation?

On my desk, after substitution --- simply to be clearer --- of RGR by x, Mathematica gives the result
Solve[-b LMR + LMA RGR - a LMR Log10[e LMA + (d LMA (1 - LMR) SAR)/x] == 0, x]
{{x -> ConditionalExpression[(
d LMA (-1 + LMR) SAR)/(-10^(-((b LMR - LMA RGR)/(a LMR))) +
e LMA), -\[Pi] <=
Im[(b LMR - LMA RGR)/(a LMR)] Log[10] < \[Pi]]}}
Was the final point in your expression present in your code ?

Related

Why is checking error less than 1 sufficient for Newton's method square root estimation integer?

Problem is from this link: https://leetcode.com/problems/sqrtx
My question is the line while (Math.abs(x0-x1) >= 1) condition. I don't understand why the moment that condition breaks, the answer would be the floor of x1. Is there no case where the break condition occurs at something like x1 equals 7 point something, but the true square root is 6 point something, giving the wrong answer of 7 when the actual answer is 6?
From my testing, this seems to be an invariant, but I don't understand why.
This is the Newton's Method solution:
class Solution {
public int mySqrt(int x) {
if (x < 2) return x;
double x0 = x;
double x1 = (x0 + x / x0) / 2.0;
while (Math.abs(x0 - x1) >= 1) {
x0 = x1;
x1 = (x0 + x / x0) / 2.0;
}
return (int)x1;
}
}
This is indeed clever, and not at all obvious.
The update rule we have is x1 = (x0 + x/x0)/2 = f(x0).
First question. What is f'(x0)?
f'(x0) = d/dx0 (x0 + x/x0) / 2
= (1 - x/x0^2) / 2
Let's note some facts.
If sqrt(x) < x0, then the derivative is positive. So f(x0) is strictly increasing over sqrt(x) < x0. But f(sqrt(x)) = sqrt(x). Therefore if sqrt(x) < x0 then sqrt(x) < f(x0).
The derivative is always < 1. So x0 - f(x0) is also strictly increasing. That's the size of the step.
We are treating x as a constant here. But we should also note that (as long as x0^2 < x), the larger x is, the smaller the step size gets.
Enough Calculus, let's do something harder - algebra!
Let's try our best to have it give an answer of k when it should be giving an answer of k-1. That means we want x < k*k. The larger x is, the smaller the step, so we'll make x as big as we can - namely k*k-1
We want k <= f(x0). But we also want x0 - f(x0) < 1. Keeping in mind point 2, let's try x0 = k+1 and see where our next point lands.
But
f(k+1) = f(x0)
= (x0 + x/x0) / 2
= ((k+1) + (k^2 - 1)/(k+1)) / 2
= ((k+1)^2 + (k^2 - 1)) / (2 * (k+1))
= ((k^2 + 2k + 1) + (k^2 - 1)) / (2 * (k+1))
= (2 k^2 + 2k) / (2 * (k+1))
= (2 * (k+1) * k) / (2 * (k+1))
= k
And whoops - x0 - f(x0) == 1 which fails our condition. Increasing x0 from there will result in a larger step size (which means we aren't finished yet), and decreasing it gives us the right answer.
So the condition is proven mathematically correct.
But...let's test it. Here is a test in Python.
def my_sqrt(x):
x0 = x
x1 = (x0 + x/x0)/2
while (1 <= abs(x1 - x0)):
x0 = x1
x1 = (x0 + x/x0)/2
return int(x1)
for i in range(2, 1000000):
if i == my_sqrt(i * i - 1):
print(i)
This finds that if k is in (314161, 599326, 599327, ...) then you get the wrong answer. Why? Because it is a delicate condition, and even one ulp of error can mess up the answer.

Is there a general term formula for 3 queens problem?

The specific question description is:
Put 3 queens on a chessboard of M columns and N rows, how to determine the number of ways that no two of them are in attacking positions?
Note that M is not equals to N, and M/N are larger than a Integer in C language so that there is no way to solve this question using classical computer algorithm like DFS/BFS(for time and memory complexity considerations).
I guess this problem can be calculated by the mathematical method of permutation or combination, but I am not good at math, so, please help me.
Yes.
Searching for keyword "3 queens" in OEIS gives us A047659, and in the Formula section, Vaclav Kotesovec wrote that:
In general, for m <= n, n >= 3, the number of ways to place 3 nonattacking queens on an m X n board is n^3/6*(m^3 - 3m^2 + 2m) - n^2/2*(3m^3 - 9m^2 + 6m) + n/6(2m^4 + 20m^3 - 77m^2 + 58m) - 1/24*(39m^4 - 82m^3 - 36m^2 + 88m) + 1/16*(2m - 4n + 1)(1 + (-1)^(m+1)) + 1/2(1 + abs(n - 2m + 3) - abs(n - 2m + 4))(1/24((n - 2m + 11)^4 - 42(n - 2m + 11)^3 + 656(n - 2m + 11)^2 - 4518(n - 2m + 11) + 11583) - 1/16(4m - 2n - 1)*(1 + (-1)^(n+1))) [Panos Louridas, idee & form 93/2007, pp. 2936-2938].
This formula can be manually confirmed on small Ns and Ms. A simple Python script for this purpose is shown below:
import fractions # to avoid floating error
m = fractions.Fraction(4)
n = fractions.Fraction(4)
assert m<=n
one = fractions.Fraction(1)
ans = n**3/6*(m**3 - 3*m**2 + 2*m) - n**2/2*(3*m**3 - 9*m**2 + 6*m) + n/6*(2*m**4 + 20*m**3 - 77*m**2 + 58*m) - one/24*(39*m**4 - 82*m**3 - 36*m**2 + 88*m) + one/16*(2*m - 4*n + 1)*(1 + (-1)**(m+1)) + one/2*(1 + abs(n - 2*m + 3) - abs(n - 2*m + 4))*(one/24*((n - 2*m + 11)**4 - 42*(n - 2*m + 11)**3 + 656*(n - 2*m + 11)**2 - 4518*(n - 2*m + 11) + 11583) - one/16*(4*m - 2*n - 1)*(1 + (-1)**(n+1)))
print(ans)
Unfortunately, I failed to find the proof of this formula ([Panos Louridas, idee & form 93/2007, pp. 2936-2938], as Vaclav Kotesovec cited). The journal idee & form does not seem to be freely accessible. But due to the reputation of Vaclav Kotesovec (the author of Non-attacking chess pieces), I think we should trust this result.
The simple answer is inclusion/exclusion.
We start by counting the number of ways to place 3 queens in order. Which is just (n*m) * (n*m - 1) * (n*m - 2).
Now we have overcounted, because we don't want the count of the number of ways to place 3 queens with queens 1,2 attacking. Or queens 1,3. Or queens 2,3. But that is just the sum over rows, columns and diagonals of length l of l * (l-1) * (m*n-2).
But now we have undercounted, because if all 3 queens attack each other then we added them in the first step, subtracted 3x in the second step, and now we need to add them back 2x to get to counting those 0 times. Which is the sum over rows, columns and diagonals of length l of l * (l-1) * (l-2).
But this is the count of ways to place all of the queens in order. But given 3 queens on the board, there are 6 orders we could place them. So divide the whole answer by 6.
This can be turned into a program that is O(n+m) to run. Which should be fast enough for your purposes. If you were willing to do a bunch more analysis, we could actually produce a O(1) formula.
The field with coordinates (i, j) is vulnerable for the queen locаted at (qi , qj) if
i == qi || j == qj || abs(i - j) == abs(qi - qj)
This boolean expression should be false for feasible coordinates of each queen. Finding three such fields should not be hard. One cаn try Monte Carlo method which has complexity o(M * N) in worst case.

Delphi blur Effect using VCL

I'm implementing a custom panel blur effect. I would like to simulate a glass effect.
I found the following code in my searches:
function blur(source:TBitmap):TBitmap;
var
x, y, x3:integer;
tline, mline, bline:PByteArray;
begin
for y := 1 to source.Height - 2 do
begin
tline := source.ScanLine[y-1];
mline := source.ScanLine[y];
bline := source.ScanLine[y+1];
for x := 1 to source.Width - 2 do
begin
x3 := x*3;
mline^[x3] :=
(mline^[x3 - 3] + mline^[x3 + 3] +
tline^[x3 - 3] + tline^[x3 + 3] + tline^[x3] +
mline^[x3 - 3] + mline^[x3 + 3] + mline^[x3]) div 8;
mline^[x3 + 1] :=
(mline^[x3 - 2] + mline^[x*3 + 4] +
tline^[x3 - 2] + tline^[x*3 + 4] + tline^[x3+1] +
mline^[x3 - 2] + mline^[x*3 + 4] + mline^[x3+1]) div 8;
mline^[x3 + 2] :=
(mline^[x3 - 1] + mline^[x3 + 5] +
tline^[x3 - 1] + tline^[x3 + 5] + tline^[x3+2] +
mline^[x3 - 1] + mline^[x3 + 5] + mline^[x3+2]) div 8;
end;
end;
result := source;
end;
The above function works fine, but since I am not the author and do not understand anything about image manipulation, I would add to this function the possibility of setting the blur level.
I also found the following code, which works and is configurable, but, it is quite slow to apply.
A temporary solution I found to "configure" the blur was to adjust function to run itself recursively according to the level of blur you want. See the code snippet below:
function blur(source:TBitmap; nTimes: Integer = 1): TBitmap;
var
x, y, x3, I : integer;
tline, mline, bline: PByteArray;
begin
if nTimes = 0 then Exit(source);
for I := 0 to nTimes do...
The question is: how to make a configurable and efficient blur effect or could anyone help me to improve the above function?
This function does not work correctly, perhaps due to mistakes in copy-paste.
Blur is kind of convolution. To get value for pixel in blurred image, you have to calculate weighted sum of source pixels in some vicinity.
Dest[y,x] = Sum(dy=-size/2..size/2, dx=-size/2..size/2) (Src[y+dy, x+dx] * Weight[dy, dx])
In your case coefficients (weights) are
1/8 1/8 1/8
1/8 0 1/8 or 1/8 * (1,1,1,1,0,1,1,1,1)
1/8 1/8 1/8
This is not Gaussian blur - its coefficients for 3x3 kernel are 1/16 * (1, 2, 1, 2, 4, 2, 1, 2, 1). Note that central pixel is involved with the largest coefficient.
Moreover, code does not exploit lower scanline - the third line in every sum should use bline.
In general, you can calculate and use Gaussian kernel of any size (formula depending on sigma is described everywhere), but direct use of large-sized filter is slow. Multiple applying of small Gaussian filter to the same image is equivalent to applying of some larger filter.
For large kernel size there is fast (but more complicated) approach - fast convolution through FFT (fast Fourier transform).

finding point on a line which is equidistant from 2 other points

I know I probably should have asked this question in math.stackexchange but answers there are mostly "pen-paper" type. I need an efficient approach to implement and that is why I am asking here.
Q: Given 2 points A & B and a line L. Points and line are 3D. How to find a point on the given line L which is equidistant from the given points A & B?
The approach I followed is:
finding plane, P, perpendicular to line AB and passing through the center of A & B.
point of intersection of P and L would be the answer.
I am working on large data-set (a 3d image) and doing the above calculations involves large no. of multiplications and divisions (on total).
So is there a better way of doing it.
A working code would be very helpful.
Thanks in advance.
If line L is described by parametric vector equation
P=C+t*D
(where C is some base point, D is direction vector, t is parameter)
then point P is equidistant from the given points A & B, when vector from P to the middle of A-B segment is perpendicular to AB vector. So scalar product of these vectors is zero.
(B-A)*(C+t*D-(A+B)/2)=0
Let's
F=B-A
G=C-(A+B)/2
then (in coordinate form)
Fx*(Gx+t*Dx)+Fy*(Gy+t*Dy)+Fz*(Gz+t*Dz)=0
t*(Dx*Fx+Fy*Dy+Fz*Dz)=-(Fx*Gx+Fy*Gy+Fz*Gz)
t=-(Fx*Gx+Fy*Gy+Fz*Gz)/(Dx*Fx+Fy*Dy+Fz*Dz)
case of (Dx*Fx+Fy*Dy+Fz*Dz)=0 corresponds to perpendicular lines AB and L. In this case there is no solution when nominator (Fx*Gx+Fy*Gy+Fz*Gz) is non-zero, and there is infinity of solutions, if nominator is zero (all the points on line are equidistant)
TL;DR. The solution is at the bottom.
Alright, so let's say these are our parameters:
x0 and x1, that describe the line.
a and b, the two points.
Where
x0 = [x0, y0, z0]
x1 = [x1, y1, z1]
a = [xa, ya, za]
b = [xb, yb, zb]
δ = [δx, δy, δz] = (x1 - x0)
Then our description of the line can be seen as a parametric function:
l(λ) = x0 + λ*(x1 - x0)
So we are trying to find a value for λ that satisfies the following equation:
(l(λ) - a)2 = (l(λ) - b)2
(Here I'm cheating with notation a bit, so x2 = x.x)
So expanding everything we get:
(λx1 + (1 - λ)x0 - xa)2 +
(λy1 + (1 - λ)y0 - ya)2 +
(λz1 + (1 - λ)z0 - za)2 =
(λx1 + (1 - λ)x0 - xb)2 +
(λy1 + (1 - λ)y0 - yb)2 +
(λz1 + (1 - λ)z0 - zb)2
Simplifying, we get:
(λδx + x0 - xa)2 +
(λδy + y0 - ya)2 +
(λδz + z0 - za)2 =
(λδx + x0 - xb)2 +
(λδy + y0 - yb)2 +
(λδz + z0 - zb)2
Expanding the brackets and cancelling, we get:
-2δxxaλ + (x0 - xa)2 +
-2δyyaλ + (y0 - ya)2 +
-2δzzaλ + (z0 - za)2 =
-2δxxbλ + (x0 - xb)2 +
-2δyybλ + (y0 - yb)2 +
-2δzzbλ + (z0 - zb)2
Which we can simplify further into some nice neat vector operations:
2λδ.(xb - xa) =
(x0 - xb)2 -
(x0 - xa)2
Which is pretty simple to rearrange to get a linear equation in λ with one unique solution.
You can solve a system of equations to get the answer. A line in 3D is something of the sort of:
x = x<sub>0</sub> + t * v<sub>x</sub>
y = y<sub>0</sub> + t * v<sub>y</sub>
z = z<sub>0</sub> + t * v<sub>z</sub>
Let's denote the point we are searching for P. Now we have:
(Px - Ax)2 + (Py - Ay)2 +(Pz - Az)2 = (Px - Bx)2 +
(Py - By)2 + (Pz - Bz)2
Note that although all these equations seem to be quadtratic, the square of Px we be scratched because it appears on both sides of the equation. Now use this system of equations and the ones above to find the three variables Px, Py and Pz.

Getting the equation for the line of intersection using Mathematica

I have a nasty expression that I am playing around with on Mathematica.
(-X + (2 X - X^2)/(
2 (-1 + X)^2 ((1 + 2 (-1 + p) X - (-1 + p) X^2)/(-1 + X)^2)^(3/2)))/X
I graphed it along with the plane z = 0 where X and p are both restricted from 0 to 1:
Plot3D[{nasty equation is here, 0}, {p , 0, 1}, {X, 0, 1}]
I decided it would be interesting to obtain the equation for the intersection of the plane generated from the nasty equation and z = 0. So I used solve:
Solve[{that nasty equation == 0}, {p, X}, Reals]
and the output was even nastier with some results having the # symbol in it ( I have no idea what it is, and I am new to Mathematica). Is there a way to get an equation for the nice line of intersection between the nasty equation and z = 0 where p and X are restricted from 0 to 1? In the graph generated from Plot3D I see that the line of intersection appears to be some nice looking half parabola looking thing. I would like the equation for that if possible. Thank you!
For complicated nasty equations Reduce is often more powerful and less likely to give you something that you will later find has hidden assumptions inside the result. Notice I include your constraint about the range of p and X to give Reduce the maximum amount of
information that I can to help it produce the simplest possible solution for you.
In[1]:= Reduce[(-X + (2 X-X^2)/(2 (-1 + X)^2 ((1 + 2 (-1 + p) X - (-1 + p) X^2)/
(-1 + X)^2)^(3/2)))/X == 0 && 0 < X < 1 && 0 < p < 1, {X, p}]
Out[1]= 0<X<1 && p == Root[12 - 47*X + 74*X^2 - 59*X^3 + 24*X^4 - 4*X^5 + (-24 +
108*X - 192*X^2 + 168*X^3 - 72*X^4 + 12*X^5)*#1 + (-48*X + 144*X^2 - 156*X^3 +
72*X^4 - 12*X^5)*#1^2 + (-32*X^2 + 48*X^3 - 24*X^4 + 4*X^5)*#1^3 & , 1]
Root is a Mathematica function representing a root of a usually complicated polynomial
that would often be much larger if the actual root were written out in algebra, but we
can see whether the result is understandable enough to be useful by using ToRadicals.
Often Reduce will return several different alternatives using && (and) and || (or) to
let you see the details you must understand to correctly use the result. See how I
copy the entire Root[...] and put that inside ToRadicals. Notice how Reduce returns
answers that include information about the ranges of variables. And see how I give Simplify the domain information about X to allow it to provide the greatest possible simplification.
In[2]:= Simplify[ToRadicals[Root[12 - 47 X + 74 X^2 - 59 X^3 + 24 X^4 - 4 X^5 +
(-24 + 108 X - 192 X^2 + 168 X^3 - 72 X^4 + 12 X^5) #1 + (-48 X + 144 X^2 -
156 X^3 + 72 X^4 - 12 X^5) #1^2 + (-32 X^2 + 48 X^3 - 24 X^4+ 4 X^5)#1^3&,1]],
0 < X < 1]
Out[2]= (8*X - 24*X^2 + 26*X^3 - 12*X^4 + 2*X^5 + 2^(1/3)*(-((-2 + X)^8*(-1 +
X)^2*X^3))^(1/3))/(2*(-2 + X)^3*X^2)
So your desired answer of where z= 0 will be where X is not zero, to avoid 0/0 in
your original equation, and where 0 < X < 1, 0 < p < 1 and where p is a root of that
final complicated expression in X. That result is a fraction and to be a root you
might take a look at where the numerator is zero to see if you can get any more
information about what you are looking for.
Sometimes you can learn something by plotting an expression. If you try to plot that final result you may end up with axes, but no plot. Perhaps the denominator is causing problems. You can try plotting just the numerator. You may again get an empty plot. Perhaps it is your cube root giving complex values. So you can put your numerator inside Re[] and plot that, then repeat that but using Im[]. Those will let you plot just the real and imaginary parts. You are doing this to try to understand where the roots might be. You should be cautious with plots because sometimes, particularly for complicated nasty expressions, the plot can make mistakes or hide desired information from you, but when used with care you can often learn something from this.
And, as always, test this and everything else very carefully to try to make sure that no mistakes have been made. It is too easy to "type some stuff into Mathematica, get some stuff out", think you have the answer and have no idea that there are significant errors hidden.

Resources