Techniques for simplifying expressions – Mathematica Stack Exchange

I am hoping to get techniques to use when faced with expressions I think may be simplified.
From two symmetrical looks at a problem I ended up with two inequalities. With excess stuff removed and everything is a non-negative integer:

$xledelta(A_s,X_v)+delta(z,X_s)$ and $xledelta(X_s,A_v)+delta(z,A_s)$

0 & z=2^{j}\
1 & otherwise
and $delta(x,y,ldots)=delta(x)delta(y,…)$

It would be nice to just have a single bound for $x$ if it’s not complicated.

So I define delta as:

d(x_) := If(DigitCount(x, 2, 1) == 1, 0, 1)

I want to simplify:

x <= d(Xs)*d(Av) + d(z)*d(As) && x <= d(As)*d(Xv) + d(z)*d(Xs)

or maybe

Min(d(Xs)*d(Av) + d(z)*d(As), d(As)*d(Xv) + d(z)*d(Xs))

What can I use to investigate this?

matrix – Making Mathematica use the cyclicity of the trace to simplify expressions

You can try the following formulation of the trace and dot product that can be simplified using TensorReduce

Assuming(Element(a | b, Matrices({n, n})), 
  TensorContract(TensorProduct(a,b), {{2, 3}, {1, 4}}) - 
   TensorContract(TensorProduct(b,a), {{2, 3}, {1, 4}})))

This proves $$mathrm{Tr}(acdot b)=mathrm{Tr}(bcdot a).$$

A more complicated example would be

Assuming(Element(a | b | c, Matrices({n, n})), 
  TensorContract(TensorProduct(a, b, c), {{2, 3}, {4, 5}, {1, 6}}) - 
   TensorContract(TensorProduct(b, c, a), {{2, 3}, {4, 5}, {1, 6}})))

which proves

$$mathrm{Tr}(acdot bcdot c)=mathrm{Tr}(bcdot ccdot a).$$

Manipulate[] with interdependent Locators – Mathematica Stack Exchange

how would I create a Manipulate to draw a InfiniteLine between two Locator?
The first Loactor should control the x/y of the first point together with the y of the second one.
The second Locator should control x/y of the second point only.

In other words the first Locator should parallel shift the line in y, the second one change the slope.

This is what I tried so far (track the changes of first point y and update second point):

   If(p1((2)) =!= old,
     p2((2)) += p1((2)) - old;
     old = p1((2));
  InfiniteLine({p1, p2}),
  PlotRange -> 4 {{-1, 1}, {-1, 1}}
 {{old, 0}, ControlType -> None},
 {{p1, {0, 0}}, Locator},
 {{p2, {2, 1}}, Locator}

enter image description here

This “works” somehow but only when just slowly dragging the first point, otherwise it gets garbled.

Any hints wellcome.

How to prove the following optimization result from Mathematica?

Currently, I am working on a decision problem in Telecommunication. This problem is reformulated under a form of Optimization problem that is:

{text{Minimize}},,,A hfill \
st.,,,frac{{aleft( {left( {{t^2} – 1} right){S_1} + {{left( {frac{B}{A}} right)}^2}{{left( {{t^2} + 1} right)}^2} – 4{t^2}} right)}}{{left( {{{left( {frac{B}{A}} right)}^2} – 1} right){{left( {{t^2} + 1} right)}^2}}} + Dleft( {frac{2}{{1 + {t^2}}} – 1} right) leqslant frac{{2t}}{{{{left( {{t^2} + 1} right)}^2}}}left( {aleft( {1 – {t^2}} right) – {S_2}} right) hfill \
end{gathered} $$

Where $a > 0,A>0, B > 0, C > 0, 0 < frac{B}{A} < 1$ and the term ${S_1}$ and ${S_2}$ are given as:

{S_1}^2 = {left( {frac{B}{A}} right)^2}{left( {{t^2} + 1} right)^2} – 4{t^2} hfill \
{S_2}^2 = {left( {frac{1}{{BC}}} right)^2}{left( {{t^2} + 1} right)^2} – 4{a^2}{t^2} hfill \
end{gathered} $

Note that the term $D$ is guarantee to be negative ($D<0$). Also, the term $t = tan left( {frac{theta }{2}} right)$ where the angle $0 < theta < frac{pi }{2}$ which lead to $0 < t < 1$.

Practically speaking, there is also a constraint for how large and how small the value of $A$ should be that is $0 < {M_L} < A < {M_U}$. However, the physical range $left| {{M_U} – {M_L}} right|$ is quite large so this constraint can be dropped.

The followings is the code for invoking Mathematica parametric optimization:

Minimize({A,(a*((t^2-1)Subscript(S, 1)+(B/A)^2 (t^2+1)^2-4t^2))/(((B/A)^2-1)*Power(t^2+1,2))+D*(2/(1+t^2)-1)<= (2*t)/Power(t^2+1,2) (a*(1-t^2)-Subscript(S, 2))&&D<0&&0<t<1 &&Power(Subscript(S, 2),2)==Power(1/(B*C),2)*(t^2+1)^2-4*a^2*t^2&&a>0&&B>0&&C>0&&A>0&&0<B/A<1&&Power(Subscript(S, 1),2)==(B/A)^2*(t^2+1)^2-4*t^2&&0<Subscript(M, L)<A<Subscript(M, U)&&Subscript(S, 1)>0&&Subscript(S, 2)>0},{A})

Which give the minimizer is $A = sqrt {frac{{{B^2}{{left( {1 + {t^2}} right)}^2}}}{{4{t^2} + {{left( {{S_1}} right)}^2}}}}$. An interesting fact is that Mathematica show that this minimizer can be achieve in the following cases:

Cases 1:
$t = – 1 + sqrt 2 $ this means that $theta = frac{pi }{4}$

Cases 2:
$0 < t < – 1 + sqrt 2 $ so $0 < theta < frac{pi }{4}$

Cases 3:
$ – 1 + sqrt 2 < t < 1$ so $frac{pi }{4} < theta < frac{pi }{2}$

Among all of these cases, this equality happens $C = sqrt {frac{{1 + 2{t^2} + {t^4}}}{{{B^2}left( {4{a^2}{t^2} + {S_2}^2} right)}}} $. I guess that this is an important result to construct the proof.

How to proof that this result is correct since Mathematica hide all the reasoning step ?

Thank you for your enthusiasm !

list manipulation – Python recursive bisection algorithm to Mathematica

I’ve been sitting on this problem for a while and i am kind of stuck. I am pretty new to Mathematica and have been trying to implement a recusive Algorithm, which splits/bisects a list of items in parts recursively. This means, that when the algorithm loops back it should bisect both already bisected parts. The algorithm stops, when there are no elements left to bisect. Regullary I would do after every bisection some calculation which is not important for this question. More importantly is the question how to handle multiple for loops in Mathematica like it is being done in python.

I have an Python-Code which illustrates the algorithm by printing the bisectionsteps. The first picture shows the algorithm without recursion, to illustrate the first iteration. In the second picture I did it with the while loop and print comand so you can see the functionality. As you can see, j iterates the left side, while k iterates through the right side. As I special problem I find to loop through ((0,len(i)//2) which is an array with only two elements and than plugging them in to i(j:k).enter image description hereenter image description here

Hopefully there is someone who is experienced with both programming languages and can halp me out.

Thank you in advance!

How to import NSE option chain data into mathematica

I’m trying to import option chain data into mathematica from the



however in mathematica it’s running for hours without doing anything. “URLFetch” and “URLSave” are also getting stuck at ‘Running…’

The .csv file containing the data in this


is only 12KB. If I download this file and import it in mathematica, I have to repeat the step every 1&1/2 minutes as the data gets updated every 1&1/2 minutes. Doing so continuously for 6&1/4 hrs everyday seems impossible.

Could anyone let me know how the data could be imported from the url directly. Alternatively, if I could download the .csv file automatically (if there is a way?), the purpose would be served.

nested MapThread – Mathematica Stack Exchange

I am trying to repair a list of supposedly monotonically and smoothly ascending numbers, like a CDF. An example would be like

aa={1,2,3,4,5,100,200,300,400,500,30,31,32,33,34,35}; bb=Differences[aa]

The objective is to scale up the first five elements to synch up with the sixth which is 100, and then to scale down the first ten elements to bring the first ten elements of the revised list to line up with the remaining six elements.

I have tried the following


Apparently, #1 for the first MapThread and #2 for the second MapThread were not executed as intended. Any suggestions or corrections will be much appreciated.

algebraic manipulation – Telling Mathematica to treat square roots as real valued

I often deal with problems which I know are real-valued, but Mathematica skips some simplifications because it doesn’t know which branch of Sqrt to take. Is there some generic way to tell it to treat all Sqrt (and maybe other multibranch functions?) that inputs and outputs are real-valued, for the purpose of Simplify?


I was looking to compute the following:


This doesn’t evaluate Mathematica, however, a user found that some manual algebraic manipulation it is solvable:

1/Sqrt(Product(1 + (2 s)/k^3, {k, 1, (Infinity)})) // Simplify

$$frac{1}{sqrt{frac{1}{Gamma left(1-sqrt(3){-2} sqrt(3){s}right) Gamma left(sqrt(3){2} sqrt(3){s}+1right) Gamma left(frac{i left(i+sqrt{3}right) sqrt(3){s}}{2^{2/3}}+1right)}}}$$

The downside is that Simplify doesn’t remove the reciprocals. On other hand, manually switching $1/x$ and $sqrt{x}$ operations gives the desired expression

Sqrt(1/Product(1 + (2 s)/k^3, {k, 1, (Infinity)}))

$$sqrt{Gamma left(1-sqrt(3){-2} sqrt(3){s}right) Gamma left(sqrt(3){2} sqrt(3){s}+1right) Gamma left(frac{i left(i+sqrt{3}right) sqrt(3){s}}{2^{2/3}}+1right)}$$

plotting – How to plot execute this simple code in Mathematica?

I am trying to execute this code in Mathematica but getting an error “Symbol N is Protected. >>”
The code is written below. If anyone can resolve this will be appreciated. (A document related to this code is also attached.)(!(enter image description here)(1))(1)

N = 50;
k = 2*Pi/lambda;
r0 = 0.2;
x = k*r0;
z = m1*x;
j = (1 : N);(*limit form 1 to N*)
sqr = Sqrt(Pi*x/2);
sqrm = Sqrt(Pi*z/2);
phi = sqr*BesselJ(j + 0.5, x);
xi = sqr*(BesselJ(j + 0.5, x) + I*BesselY(j + 0.5, x));
phim = sqrm*BesselJ(j + 0.5, z);
phi1 = (Sin(x), phi(1 : N - 1));
phi1m = (Sin(z), phim(1 : N - 1));
y = sqr*BesselY(j + 0.5, x);
y1 = (-Cos(x), y(1 : N - 1));

phip = (phi1 - j/x*phi);
phimp = (phi1m - j/z*phim);
xip = (phi1 + I*y1) - j/x*(phi + I*y);
Subscript(a, j) = (m1*phim*phip - phi*phimp)/(m1*phim*xip - xi*phimp);
Subscript(b, j) = (phim*phip - m1*phi*phimp)/(phim*xip - m1*xi*phimp);

sigext = !(
*UnderoverscriptBox(((Sum)), (j = 
     1), (Infinity))((((2*j + 1))*Re(((
*SubscriptBox((a), (j)) + 
*SubscriptBox((b), (j))))))));
sigsca = !(
*UnderoverscriptBox(((Sum)), (j = 
     1), (Infinity))((((2*j + 1))*((Abs(
*SubscriptBox((a), (j)))^2 + Abs(
*SubscriptBox((b), (j)))^2)))));
sigabs = sigext - sigsca;

Plot({sigext}, {lambda, 0, 10})


Piecewise Function Definition – Mathematica Stack Exchange

I am new to Mathematica, and I am unsure on how to do this.

I am working with piecewise function

f_N(x) := begin{cases} 1 & text{if};;x = jsqrt{3}, quad j=1,…,N \
0 & text{otherwise},end{cases}

for some arbitrary $N$ that I define when I call the function.

I understand that I have to use the built in function Piecewise to be able to perform this, but am not sure how to include the fact that $N$ is a variable and not have to define the function again and again for every use case.