A72 ==== Subject: Re: Wrong Integral result for a Piecewise function > This integral gives gives the wrong result for the interval [2,3] h[x_] := Integrate[Boole[x - 1 < 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] Plot[h[x],{x,0,5}] > and > Plot[Evaluate[h[x]],{x,0,5}] give different plots, The curve should be smooth and bell shaped from > 0 to 5, so the first looks correct. The second has a jump > discontinuity > at 2 and 3. Also h[5/2] > and > h[x]/.x-> 5/2 give different answers This is using some new features in Mathematica 5.1. Same result on a > Mac (10.4) and SunOS (5.9). Breaking into a difference of two integrals gives the correct answer: > Integrate[Boole[ 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] - > Integrate[Boole[ 2 y + 2 z < x-1], {y, 0, 1}, {z, 0, 1}] Finally > Integrate[h[x], {x, 0, 5}] and NIntegrate[h[x], {x, 0, 5}] > both give the wrong answer, it should be 1. The triple integral is > correct Integrate[Boole[x - 1 < 2 y + 2z < x], {y, 0, 1}, {z, 0, 1}, {x, 0, > 5}] Any suggestions on integrating over regions with linear constraints? > Versions 5.1 has powerful new piecewise functions If you try instead g[x_] = FullSimplify[Integrate[UnitStep[2y + 2z - (x - 1)]*UnitStep[x - 2y - 2z], {y, 0, 1}, {z, 0, 1}]] (this takes a while to complete) then Plot[g[x],{x,0,5}] looks correct. Also In[18]:= NIntegrate[g[x], {x, 0, 5}] Out[18]= 1. In[19]:= Integrate[g[x], {x, 0, 5}] Out[19]= 1 This, of course, is the pre-Mathematica 5 way of doing these things which only goes to confirm that progress is not always improvement ;-) Andrzej Kozlowski Chiba, Japan ==== Subject: Re: Urgent > I need help solving the Laplace equation in three dimensions using Mathematica. Due to the geometric configuration I used oblate and prolate spheroidal coordinates. > You can see the equations I'm using in: http://mathematica.no.sapo.pt/index.html > Particular solutions are possible as series of products of exponentials in phi, Legendre polynomials of the first L^m_n kind of cos theta and the second kind Q^m_n of cosh eta, also expressible by spherical harmonics Y_lm(cos theta) * Q^m_n (cosh eta). Make a product ansatz << Calculus`VectorAnalysis` SetCoordinates[OblateSpheroidal[theta, eta, phi, a]] F=F1[theta]*F2[eta]*F3[phi]] Laplacian[ F ]/F // FullSimplify // Expand and separate the terms depending on one variable only. You have to solve three eigenvalue ODE's and then you select the fourier sum over the eigenvalue-indexed products to fit your boundary values. -- Roland Franzius ==== Subject: Re: Simplify and FullSimplify A result of a calculation I was doing generated this expression.... q-q Exp[-a x] + c Exp[-a x] > naturally my next step was Simplify and I thought I'd get the Exp > [- ax] > collected.....to my complete surprize I got the following: Exp[-a x] (c + (-1+ Exp[a x]) q > How on Earth did Mathematica come up with this? I checked > FullSimplify > which did collect Exp[-a x].... On re-reading my question before I submitted it, I see that with > Simplify Mathematica 'collected' using Exp[- a x] q.....of course, > visually this expression seems quite complex and would seem to take > much > more 'thinking' to get ......why do Simplify and FullSimplify have > such > a vast difference in what is considered 'Simpler'? They do not differ at all in what is considered Simpler (Adam Strzebonski once published here the default ComplexityFunction which is the same for both) but FullSimplify applies a lot more transformation rules. I have no idea what transformation function in FullSimplify is responsible for this simplification. However, here is a transformation fucntion I have manufactured: f[expr_] := First[Sort[(Collect[expr, #1] & ) /@ Level[expr, {1, Infinity}], LeafCount[#1] <= LeafCount[#2] & ]] With this transformation function you will get: Simplify[(-E^((-a)*x))*q + q + c/E^(a*x), TransformationFunctions -> {Automatic, f}] (c - q)/E^(a*x) + q What f[expr] does is to try to Collect expr with respect to all subexpressions of expr at levels 1 to Infinity and then choose the form that has the least LeafCount. Obviously this is time consuming for complicated expressions. Possibly FullSimplify uses something similar. Andrzej Kozlowski ==== Subject: Using InterpolatingFunction from NDSolve I solved a PDE with NDSolve in Mathematica 5.1. I could plot, differentiate and integrate the obtained InterpolatingFunction object, the result being a similar object. I was able to integrate the 2nd derivative of it. What I need is to integrate the square of the obtained InterpolatingFunction object (the square itself does not simplify to such an object). You can see the details on my homepage: http://www.math.bme.hu/~tladics/nds.nb Every suggestions are welcome! Tam.87s ==== Subject: Re: Using InterpolatingFunction from NDSolve > I solved a PDE with NDSolve in Mathematica 5.1. I could plot, > differentiate and integrate the obtained InterpolatingFunction object, > the result being a similar object. I was able to integrate the 2nd > derivative of it. What I need is to integrate the square of the > obtained InterpolatingFunction object (the square itself does not > simplify to such an object). > You can see the details on my homepage: > http://www.math.bme.hu/~tladics/nds.nb Every suggestions are welcome! Tam=E1s > In[1]:=g uSol :=g u /. First[NDSolve[ {D[u[t, x], {t, 1}] =g= D[u[t, x], {x, 2}] + u[t, x]^2 - u[t, x], u[0, x =g= Sin[x], u[t, 0] == 0, u[t, 2*Pi] == 0}, u, {t, 0, 1}, {x, 0, 2*Pi}]] In[2]:=g intSquare[s_?NumericQ, x_?NumericQ] :=g NIntegrate[uSol[t, x]^2, {t, 0, s}, MaxRecursion -> 20] In[3]:=g Plot3D[intSquare[s, x], {s, 0, 1}, {x, 0, 2*Pi}]; works ok on my PC. -- http://people.freenet.de/Peter_Berlin/ ==== Subject: Re: Using InterpolatingFunction from NDSolve Hi Tam.87s, It seems that InterpolatingFunction is not very well integrated in the Integrate function. Integrate can not even handle very simple functions of InterpolatingFunction. Example: We define an interpolating function (1 dimensionla for simplicity): f = Interpolation[{{0, 0}, {1, 1}, {2, 2}, {3, 3}}] The following works Integrate[f[x],{x,0,3}]] It also work if f is put inside a Plus function: Integrate[ Plus[f[x],f[x]] ,{x,0,3}]] however,if we change the Plus function to Times function, Integrate can no more do it: Integrate[ Times[f[x],f[x]] ,{x,0,3}]] Now what can we do? A work around is to re-interpolate: e.g. to get the f^2 we coud write: ff= FunctionInterpolation[f[x]^2, {x, 0, 3}] This function will then integrate: Integrate[ff[x],{x,0,3}]] for your case where you use replacement: u[t, x]^2 /. nds[[1]], note that FunctionInterpolation has the Attribute HoldAll. Therefore, either you replace outside FunctionInterpolation or you wrap the replacement inside Evaluate. > I solved a PDE with NDSolve in Mathematica 5.1. I could plot, > differentiate and integrate the obtained InterpolatingFunction object, > the result being a similar object. I was able to integrate the 2nd > derivative of it. What I need is to integrate the square of the > obtained InterpolatingFunction object (the square itself does not > simplify to such an object). > You can see the details on my homepage: > http://www.math.bme.hu/~tladics/nds.nb Every suggestions are welcome! Tam.87s > ==== Subject: Re: Using InterpolatingFunction from NDSolve since you avoid to give us an example here is how to do it in a 1d case: Needs[DifferentialEquations`NDSolveUtilities`] sol = x[t] /. NDSolve[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x[t], {t, 0, 2Pi}][[1]]; sqrsol = Interpolation[ Transpose@{DifferentialEquations`NDSolveUtilities`Private`GetTimeData[sol], DifferentialEquations`NDSolveUtilities`Private`GetGridData[sol]^2}][t] and here is the square of the solution Plot[sqrsol, {t, 0, 2Pi}] Jens Tam.87s schrieb im >I solved a PDE with NDSolve in Mathematica 5.1. I >could plot, > differentiate and integrate the obtained > InterpolatingFunction object, > the result being a similar object. I was able to > integrate the 2nd > derivative of it. What I need is to integrate > the square of the > obtained InterpolatingFunction object (the > square itself does not > simplify to such an object). > You can see the details on my homepage: > http://www.math.bme.hu/~tladics/nds.nb Every suggestions are welcome! Tam.87s > ==== Subject: Re: Using InterpolatingFunction from NDSolve I added a couple of definitions at the end, but most importantly, I used NIntegrate rather than Integrate. The latter fails in its attempt to find an exact expression for the integral. Kevin !(nds = NDSolve[{ D[u [t, x], {t, 1}] == D[u [t, x], {x, 2}] + u[t, x]^2 - u[t, x], u[0, x] == Sin[x], u[t, 0] == 0, u[t, 2 .93“] == 0}, u[t, x], {t, 0, 1}, {x, 0, 2 .93“}]n Plot3D[Evaluate[u[t, x] /. nds[([)(1)(])]], {t, 0, 1}, {x, 0, 2 .93“}]n Plot3D[Evaluate[Integrate[((u[t, x] /. nds[([)(1)(])])), {t, 0, s}]], {s, 0, 1}, {x, 0, 2 .93“}]n Plot3D[Evaluate[D[u[t, x] /. nds[([)(1)(])], {x, 2}]], {t, 0, 1}, {x, 0, 2 .93“}]n Integrate[D[u[t, x] /. nds[([)(1)(])], {x, 2}], {t, 0, s}]n Plot3D[Evaluate[Integrate[D[u[t, x] /. nds[([)(1)(])], { x, 2}], {t, 0, s}]], {s, 0, 1}, {x, 0, 2 .93“}]n[IndentingNewLine] u[t, x]^2 /. nds[([1])]n Plot3D[Evaluate[u[t, x]^2 /. nds[([)(1)(])]], {t, 0, 1}, {x, 0, 2 .93“}, AxesLabel -> {t, x, u[t, x]}]n u2[t_, x_] = u[t, x]^2 /. nds[([1])]n f[x_, s_] := NIntegrate[u2[t, x], {t, 0, s}]n Plot3D[f[x, s], {s, 0, 1}, {x, 0, 2 .93“}]n f[1, .5]) > I solved a PDE with NDSolve in Mathematica 5.1. I could plot, > differentiate and integrate the obtained InterpolatingFunction object, > the result being a similar object. I was able to integrate the 2nd > derivative of it. What I need is to integrate the square of the > obtained InterpolatingFunction object (the square itself does not > simplify to such an object). > You can see the details on my homepage: > http://www.math.bme.hu/~tladics/nds.nb Every suggestions are welcome! Tamķēs > ==== Subject: Re: Simplify and FullSimplify Jerry, The example you give doesn't quite rise to the requirements of the answer I am going to give because there are simple ways to do the manipulation. Nevertheless, I am going to take the question as a generic question on how one can manipulate expressions to the precise form that one may want. The trouble with Simplify and FullSimplify is that although they are very powerful and often give one a useful form, they are still a little beyond the control of the user. One can often control simplifications and manipulations much easier by working on only parts of expressions. So one could use MapAt to work on only a subpart of an expression. But Mathematica has a missing feature here. Sometimes one might want to work on only a subset of level parts, especially level parts of a sum or product. There is no routine to do that. One could write replacement rules for this, but if the expressions are long this can be time consuming. The following routines, MapLevelParts and MapLevelPatterns, are part of the Tensorial tensor calculus package. I have found them very useful in manipulating complex expressions. MapLevelParts::usage = MapLevelParts[function, {topposition, levelpositions}][expr] will map the function onto the selected level positions in an expression. Levelpositions is a list of the selected parts. The function is applied to them as a group and they are replaced with a single new expression. Other parts not specified in levelpositions are left unchanged.nExample:na + b + c + d + e // MapLevelParts[f, {{2,4,5}}] -> a + c + f[b + d + e]; MapLevelParts[func_, part : {toppart___Integer?Positive, subp : {_Integer?Positive, eprest__Integer?Positive}}][expr_] := Module[{work, subparts, npos, null, i, nnull = Length[{eprest}]}, work = func@Part[expr, Sequence @@ part]; subparts = Thread[{toppart, subp}]; newparts = {work, Table[null[i], {i, 1, nnull}]} // Flatten; npos = Partition[Range[nnull + 1], 1]; ReplacePart[expr, newparts, subparts, npos] /. null[_] -> Sequence[] ] MapLevelPatterns::usage = MapLevelPatterns[function, {topposition, {pattern}}][expr] will map the function onto the selected level positions at topposition that match the pattern in an expression. The function is applied to these positions as a group and they are replaced with a single new expression. Other parts not specified in the level are left unchanged.nExample:nf[1] + f[2] + f[3] + f[4] // MapLevelPatterns[g, {{f[_?EvenQ]}}] -> f[1] + f[3] + g[f[2] + f[4]]; MapLevelPatterns::nomatch = There were no matches for `` in ``; MapLevelPatterns[func_, {toppart___Integer?Positive, {pattern_}}][expr_] := Module[{work, levelpos}, work = Part[expr, toppart]; levelpos = Flatten@Position[work, pattern, {1}]; If[Length[levelpos] == 0, Message[MapLevelPatterns::nomatch, pattern, work]; expr, expr // MapLevelParts[func, {toppart, levelpos}]]] Let's try your expression. Simplify did not give the desired form, FullSimplify did. One could also use collect. expr = q - q Exp[-a x] + c Exp[-a x]; Simplify[expr] FullSimplify[expr] Collect[expr, Exp[-a x]] One could also use MapLevelParts to apply Simplify to only the Exp terms. expr // MapLevelParts[Simplify, {{1, 3}}] (c - q)/E^(a*x) + q Here is a more complicated expression. expr2 = Cos[t]^2 + 1/((Exp[a*t] + 1/Sqrt[h])*Cos[t]^2 + (-a + Exp[a*t] + 1/Sqrt[h])*Sin[t]^2 + c); Suppose we just want to simplify the common (Sin[t]^2 + Cos[t]^2) factor leaving a Sin[t]^2 term. FullSimplify does not give us that form. But by mapping Simplify onto just two of the terms we get the answer we want. expr2 // FullSimplify % // MapLevelParts[Simplify, {2, 1, {1, 5}}] Cos[t]^2 + 1/(-(a/2) + c + E^(a*t) + 1/Sqrt[h] + (1/2)*a*Cos[2*t]) Cos[t]^2 + 1/(c + E^(a*t) + 1/Sqrt[h] - a*Sin[t]^2) Or we could have expanded expr2 and then mapped Simplify to the desired terms. expr2 // ExpandAll % // MapLevelParts[Simplify, {2, 1, {2, 3, 5, 6}}] Cos[t]^2 + 1/(c + E^(a*t)*Cos[t]^2 + Cos[t]^2/Sqrt[h] - a*Sin[t]^2 + E^(a*t)*Sin[t]^2 + Sin[t]^2/Sqrt[h]) Cos[t]^2 + 1/(c + E^(a*t) + 1/Sqrt[h] - a*Sin[t]^2) We could also have simplified this using two MapAt statements. Here Simplify took a different path because we factored out the trig identity. We could also have mapped right onto the trig identity. MapAt[Collect[#1, E^(a*t) + 1/Sqrt[h]] & , expr2, {{2, 1}}] MapAt[Simplify, %, {{2, 1, 3, 2}}] Cos[t]^2 + 1/(c - a*Sin[t]^2 + (E^(a*t) + 1/Sqrt[h])* (Cos[t]^2 + Sin[t]^2)) Cos[t]^2 + 1/(c + E^(a*t) + 1/Sqrt[h] - a*Sin[t]^2) In any case, simplifying specific parts of an expression, or selected terms in a sum or factors in a product gives a much more controllable situation. David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ A result of a calculation I was doing generated this expression.... q-q Exp[-a x] + c Exp[-a x] naturally my next step was Simplify and I thought I'd get the Exp[- ax] collected.....to my complete surprize I got the following: Exp[-a x] (c + (-1+ Exp[a x]) q How on Earth did Mathematica come up with this? I checked FullSimplify which did collect Exp[-a x].... On re-reading my question before I submitted it, I see that with Simplify Mathematica 'collected' using Exp[- a x] q.....of course, visually this expression seems quite complex and would seem to take much more 'thinking' to get ......why do Simplify and FullSimplify have such a vast difference in what is considered 'Simpler'? ==== Subject: Don't understand behaviour of Solve[] Hallo, I have a problem understanding the general behaviour of the Solve[] function. I have a set of equations with the variables a,b,c,d and k. Evaluating Solve[set,k] or Solve[set,{a,b}] returns with {}, whereas Solve [set,{k,b,c,d}] returns solutions for k,b,c and d. My question: Why does Solve returns no solution when searching only for k for example and why is there a solution for several variables? I thought that giving a list of variables just means that I want to search for all of them. But the procedure doesn't seem to be independant from one variable to another. What kind of information contains my list of variables except my wish to solve for these? Oliver friedrich ==== Subject: Re: Don't understand behaviour of Solve[] say you have s system of equations x+y==1 x-y==0 if you solve it for y only you get y->1-x and y->x this is an contradiction because for a general x this is not true, while if you solve it for {x,y} you get the solution {x->1/2,y->1/2} Jens Oliver Friedrich schrieb im Newsbeitrag > Hallo, I have a problem understanding the general > behaviour of the Solve[] > function. I have a set of equations with the variables > a,b,c,d and k. Evaluating Solve[set,k] or Solve[set,{a,b}] > returns with {}, whereas Solve > [set,{k,b,c,d}] returns solutions for k,b,c and > d. My question: Why does Solve returns no solution > when searching only for k > for example and why is there a solution for > several variables? I thought > that giving a list of variables just means that > I want to search for all of > them. But the procedure doesn't seem to be > independant from one variable to > another. What kind of information contains my list of > variables except my wish to > solve for these? > Oliver friedrich > ==== Subject: Re: Partial diff equations computer algebra systems, but for the particular case of a gradient vector field there is a very straightforward solution procedure. The use of mathematica was sort of required because the PDEs I wanted to solve had more than a hundred terms each. The technique outlined by Devendra worked like a charm. I should have mentioned that the system I wanted to integrate was actually a gradient vector field (i.e. Curl is nul). And so the first solution method mentioned was perfect for this type of system, as it's basically the way a human would do it. My system was of dim 3, so the solution procedure went like this: sys = {fx, fy, fz} < g] sol2 = DSolve[(D[f[x, y, z], y] /. sol1[[1]]) == sys[[2]], g[1], {y,z}, GeneratedParameters -> h] sol3 = DSolve[(D[f[x, y, z], z] /. sol1[[1]] /. sol2[[1]]) == sys[[3]], h[1], {z}] f[x,y,z] /. sol1[[1]] /. sol2[[1]] /. sol3[[1]] Which i suppose could be automated in future releases of mathematica. The only problem was that while working on sol3 my computer ran out of memory. My whole system froze up. I'll try different variations of the above, not forgetting to use MemoryConstrained. Also, is there a way to tell mathematica to be more conservative in its memory usage? David Boily Centre for Intelligent Machines McGill University Montreal, Quebec ==== Subject: Re: Is Apart[ .. ] the correct way for denominator splitting? The reason Apart doesn't work is because x^2-1 can't be factored with rational coefficients. You can however use the Extension feature of Factor to do what you want: Apart2[exp_, x_] := Apart[Factor[exp, Extension -> x /. Solve[Denominator[exp] == 0, x][[1]]]] ==== Subject: Re: Is Apart[ .. ] the correct way for denominator splitting? Factor[2/(2x^2 - 1), Extension -> Sqrt[2]] // Apart gives -(Sqrt[2]/(Sqrt[2] - 2*x)) - Sqrt[2]/(Sqrt[2] + 2*x) Steve Luttrell When I ask for: Apart[2/(x^2-1), x] I nicely get: 1/(x-1) - 1/(x+1) But unfortunately Apart[2/(2x^2-1), x] does not give the same > splitting of the denominator ( it just gives back 2/(2x^2-1) ). What is the correct way to see the denominator splitting with all > terms first order (as one would use for integration, etc.) I found Andrzej Kozlowski's answer in message > from 2001, but I was wondering whether in the mean time a built-in > function might have been added? -- Jos > . > ==== Subject: Re: Simplify and FullSimplify >A result of a calculation I was doing generated this expression.... >q-q Exp[-a x] + c Exp[-a x] >naturally my next step was Simplify and I thought I'd get the >Exp[- ax] collected.....to my complete surprize I got the >following: >Exp[-a x] (c + (-1+ Exp[a x]) q >How on Earth did Mathematica come up with this? I checked >FullSimplify which did collect Exp[-a x].... >On re-reading my question before I submitted it, I see that with >Simplify Mathematica 'collected' using Exp[- a x] q.....of course, >visually this expression seems quite complex and would seem to take >much more 'thinking' to get ......why do Simplify and FullSimplify >have such a vast difference in what is considered 'Simpler'? The issue isn't a difference between what is considered simpler by Simplify and FullSimplify. Instead, FullSimplify tries more transformations than Simplify. For any method to simplify an expression, there will always be a trade between execution time and results. Simplify tries fewer transformations, enhancing execution time at the cost of not finding a transformation that may make the expression simpler. FullSimplify tries more transformations, significantly increasing the probability of finding a transformation that leads to a simpler expression at the cost of execution time. -- To reply via email subtract one hundred and four ==== Subject: Re: Wrong Integral result for a Piecewise function > This integral gives gives the wrong result for the interval [2,3] h[x_] := Integrate[Boole[x - 1 < 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] Plot[h[x],{x,0,5}] > and > Plot[Evaluate[h[x]],{x,0,5}] give different plots, The curve should be smooth and bell shaped from > 0 to 5, so the first looks correct. The second has a jump discontinuity > at 2 and 3. Also h[5/2] > and > h[x]/.x-> 5/2 give different answers This is using some new features in Mathematica 5.1. Same result on a > Mac (10.4) and SunOS (5.9). Breaking into a difference of two integrals gives the correct answer: > Integrate[Boole[ 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] - > Integrate[Boole[ 2 y + 2 z < x-1], {y, 0, 1}, {z, 0, 1}] Finally > Integrate[h[x], {x, 0, 5}] and NIntegrate[h[x], {x, 0, 5}] > both give the wrong answer, it should be 1. The triple integral is > correct Integrate[Boole[x - 1 < 2 y + 2z < x], {y, 0, 1}, {z, 0, 1}, {x, 0, 5}] Any suggestions on integrating over regions with linear constraints? > Versions 5.1 has powerful new piecewise functions > Have a look at this thread from a few days ago: http://forums.wolfram.com/mathgroup/archive/2005/Jul/msg00042.html . The same two recipes apply here: either use PiecewiseIntegrate In[1]:= PiecewiseIntegrate[Boole[x - 1 < 2*y + 2*z < x], {y, 0, 1}, {z, 0, 1}] Out[1]= If[x == 3, 3/8, 0] + If[0 < x < 1, x^2/8, 0] + If[1 <= x <= 2, (1/8)*(-1 + 2*x), 0] + If[2 < x < 3, (1/8)*(-9 + 10*x - 2*x^2), 0] + If[Inequality[3, Less, x, LessEqual, 4], (1/8)*(9 - 2*x), 0] + If[4 < x < 5, (1/8)*(-5 + x)^2, 0] or integrate separately for ranges of the parameter values where the answer can be given as a single expression without conditions: In[2]:= Piecewise[ {Assuming[#, Integrate[Boole[x - 1 < 2*y + 2*z < x], {y, 0, 1}, {z, 0, 1}]], #}& /@ (Flatten[#, 1]&)@ Table[{i < x < i + 1, x == i + 1}, {i, 0, 4}] ] // PiecewiseExpand Out[2]= Piecewise[{{1/8, x == 1 || x == 4}, {3/8, x == 2 || x == 3}, {(1/8)*(9 - 2*x), 3 < x < 4}, {(1/8)*(-5 + x)^2, 4 < x < 5}, {x^2/8, 0 < x < 1}, {(1/8)*(-1 + 2*x), 1 < x < 2}, {(1/8)*(-9 + 10*x - 2*x^2), 2 < x < 3}}] This integral also can be derived from the general convolution formula. If xi_1, ..., xi_n are independent and uniformly distributed on [0, 1], then the CDF of their sum is given as F[n_, x_] = (1/n!)*Sum[(-1)^k*Binomial[n, k]*(x - k)^n*UnitStep[x - k], {k, 0, n}] Then the integral in question equals F[2, x/2] - F[2, (x - 1)/2]. Also note that if h[x] is called inside Plot, it still uses Integrate but with an approximate integrand (after substituting a numerical value for x), which is not the same as using NIntegrate. Besides, NIntegrate in versions 5.0 and 5.1 pre-evaluates the integrand, so the numerical integration proceeds with the wrong symbolic value of h[x]. Maxim Rytin m.r@inbox.ru ==== Subject: Re: Wrong Integral result for a Piecewise function Dean, It definitely looks an Integrate bug. If you make an animation of the integrand it definitely appears that the first smooth plot is correct and the second result is incorrect. Needs[Graphics`Animation`] frame[x_] := Plot3D[Boole[x - 1 < 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}, PlotRange -> {0, 1}] Animate[frame[x], {x, 0, 5, 0.2}] SelectionMove[EvaluationNotebook[], All, GeneratedCell] FrontEndTokenExecute[OpenCloseGroup]; Pause[0.5]; FrontEndExecute[{FrontEnd`SelectionAnimate[200, AnimationDisplayTime -> 0.1, AnimationDirection -> Forward]}] David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ This integral gives gives the wrong result for the interval [2,3] h[x_] := Integrate[Boole[x - 1 < 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] Plot[h[x],{x,0,5}] and Plot[Evaluate[h[x]],{x,0,5}] give different plots, The curve should be smooth and bell shaped from 0 to 5, so the first looks correct. The second has a jump discontinuity at 2 and 3. Also h[5/2] and h[x]/.x-> 5/2 give different answers This is using some new features in Mathematica 5.1. Same result on a Mac (10.4) and SunOS (5.9). Breaking into a difference of two integrals gives the correct answer: Integrate[Boole[ 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] - Integrate[Boole[ 2 y + 2 z < x-1], {y, 0, 1}, {z, 0, 1}] Finally Integrate[h[x], {x, 0, 5}] and NIntegrate[h[x], {x, 0, 5}] both give the wrong answer, it should be 1. The triple integral is correct Integrate[Boole[x - 1 < 2 y + 2z < x], {y, 0, 1}, {z, 0, 1}, {x, 0, 5}] Any suggestions on integrating over regions with linear constraints? Versions 5.1 has powerful new piecewise functions ==== Subject: Mathematica query I am currently working in Mathematica to develope few geometry demonstrations. Can two or more lines share same endpoints? Can any operations such as drag, move etc can be done on this shared point which makes corresponding changes in all those lines? Basically can any object be shared between different objects along with its properties? I hope my query is clear. Please reply asap. ADITYA KELKAR MTech, CSE IIT,Bombay India ==== Subject: Re: Mathematica query you mean: IndexedPointSet /: Graphics[IndexedPointSet[pnts_, lst_]] := Graphics[ lst /. {Point[index_Integer] :> Point[pnts[[index]]], (h : (Line | Polygon))[index_] :> h[pnts[[#]] & /@ index] }] iset = IndexedPointSet[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, {{RGBColor[1, 0, 0], Point[1]}, {RGBColor[0, 1, 0], Line[{1, 2, 3}]}, {RGBColor[0, 0, 1], Polygon[{2, 3, 4}]}}] Show[Graphics[iset]] ?? Jens Aditya Kelkar schrieb I am currently working in Mathematica to > develope few geometry > demonstrations. Can two or more lines share same > endpoints? Can any > operations such as drag, move etc can be done on > this shared point which > makes corresponding changes in all those lines? > Basically can any object > be shared between different objects along with > its properties? I hope my > query is clear. > Please reply asap. ADITYA KELKAR > MTech, CSE > IIT,Bombay > India ==== Subject: Finite differences I am searching a finite differences program for the linear Burgers equation. What does exist on the subject? C. David ************************************************ Claire DAVID Laboratoire de Mod=E9lisation en M=E9canique Universit=E9 Paris VI Bo=EEte courrier n=B0 162 4, Place Jussieu 75005 PARIS Fax : (+33) 1.44.27.52.59 ************************************************ ==== Subject: Re: Solve gives generic solutions only. It discards solutions that are valid only when the parameters satisfy special conditions. Reduce gives the complete set of solutions. (The meaning of generic is left a bit vague but basically solutions are called generic if they are valid for infinitely many values of the parameters and not valid for at most finitely many.) So, for example: Solve[{x + y == 3, x - y == 1}, {x, y}] {{x -> 2, y -> 1}}} while Solve[{x + y == 3, x - y == 1}, x] {} The second set of equations has a solution for only when the parameter y is 1, so the set of generic solutions for x is {}. Note that Reduce[{x + y == 3, x - y == 1}, x] Out[7]= y == 1 && x == 2 Here is another example illustrating the same thing: Solve[Exp[x]*a == 2*Exp[x]*a, x] {} but Reduce[Exp[x]*a == 2*Exp[x]*a, x] a == 0 On the other hand: Solve[x/a == 2*x/a, x] {{x->0}} This time the answer is valid only when a !=0 Reduce[x/a == 2*(x/a), x] x == 0 && a != 0 but Solve ignores this because it only gives a generic answer. Andrzej Kozlowski Chiba, Japan > Hallo, I have a problem understanding the general behaviour of the Solve[] > function. I have a set of equations with the variables a,b,c,d and k. Evaluating Solve[set,k] or Solve[set,{a,b}] returns with {}, > whereas Solve > [set,{k,b,c,d}] returns solutions for k,b,c and d. My question: Why does Solve returns no solution when searching only > for k > for example and why is there a solution for several variables? I > thought > that giving a list of variables just means that I want to search > for all of > them. But the procedure doesn't seem to be independant from one > variable to > another. What kind of information contains my list of variables except my > wish to > solve for these? > Oliver friedrich ==== Subject: BKK-bound Does anybody know if there is a Mathematica implementation of the Bernstein, Khovanski, Kushnirenko bound on the number of zeros of a sparse regular polynomial system? (Actually, I think this really ought to be a built-in function.) Andrzej Kozlowski Chiba, Japan ==== Subject: Re: superscripts If it is possible, I would like to ask the following two questions: > (1) All superscripts in Mathematica default to exponents. I would like > to have the superscript become another label as with subscripts. The > reason I wish to do this is that many of matrix elements that I use > have a superscript ZERO. Consequently, Mathematica produces UNITY for > all such elements. Stan, Superscript works: In[1]:= Superscript[x, 0] Out[1]= 0 x Another possibility is to use a string 0 instead of 0: In[2]:= Power[x, 0] Out[2]= 0 x Carl Woll Wolfram Research > (2) I would like to be able to provide information to Mathematica that > helps avoid a log singularity in a definite integral. This is best > described in my attachment. [Contact the author to obtain the attachment - moderator] > ==== Subject: superscripts If it is possible, I would like to ask the following two questions: (1) All superscripts in Mathematica default to exponents. I would like to have the superscript become another label as with subscripts. The reason I wish to do this is that many of matrix elements that I use have a superscript ZERO. Consequently, Mathematica produces UNITY for all such elements. (2) I would like to be able to provide information to Mathematica that helps avoid a log singularity in a definite integral. This is best described in my attachment. [Contact the author to obtain the attachment - moderator] ==== Subject: Re: superscripts > If it is possible, I would like to ask the following two questions: > (1) All superscripts in Mathematica default to exponents. I would like > to have the superscript become another label as with subscripts. The > reason I wish to do this is that many of matrix elements that I use > have a superscript ZERO. Consequently, Mathematica produces UNITY for > all such elements. Hi Stan, You may find the following thread very interesting: _Assigning to superscripted variables_, started by Dave Snead on May 13 2002, 6:08 am (especially the use of the package Utilities`Notation` and/or the Tensor Calculus package). > (2) I would like to be able to provide information to Mathematica that > helps avoid a log singularity in a definite integral. This is best > described in my attachment. [Contact the author to obtain the attachment - moderator] > Please, send me your attachment. /J.M. ==== Subject: Courses in Spanish for learning Mathematica and LiveGraphics3D LiveGraphics3D (applet that uses output from Mathematica): Curso basico de Mathematica: http://homepage.cem.itesm.mx/lgomez/curso_basico.htm Curso avanzado de Mathematica: http://homepage.cem.itesm.mx/lgomez/curso_avanzado.htm Curso de LiveGraphics3D: http://homepage.cem.itesm.mx/lgomez/curso_lg3d.htm We hope they are useful for some of you Jose Luis ==== Subject: Re: Set of strings reducing problem I guess I have a basic question on your transformations. You give six rules for how to add the differing characters; are these the only combinations possible? For example, will the combination of strings of 1111M111 + 11112111 ever be encountered? In addition, when you say that the strings will differ from each other by only one character, does this mean in _one_ given position? For example, a transpose of characters as in 21111111 vs. 11111112 might be seen as two strings with only one differing character, or two strings of _two_ differing characters (one at either end). until nothing changes, my guess is that, yes, you've given the complete set of transformation rules. In this case, what happens if I try to decompose (someone more familiar with string manipulations, and, perhaps, crytanalysis or group theory, may be better suited to ask these questions) the operations? Can I, for example, do something like 1 + X = D ==>1 + D = 1+(1+X) =(1+1) +X =1 + X <--- Here I've assumed that because you want strings differing by characters, that (1+1) -> 1. In other words, is the addition associative? I think the answer to this hinges on the answer as to the only one character problem. Interesting problem! I'm sorry I can't help (yet) on the implementation. C.O. I have a problem that I haven't got any clue to solve with Mathematica. Let's say a have a list of n equal length strings: L={11111111, > 11112111, > 1111X111, > ... > ... > ... > 21122211} The characters used in strings are only 1, X, 2, U, M, D and >T. What I want is a reduced set of strings (with all the resulting strings >with the same length as all the original ones). The rule to join two strings is the following: If one string is different from the other by just one character then >take the characters that are different and apply the rule bellow: 1 + X = D >1 + 2 = M >1 + U = T >X + 2 = U >X + M = T >2 + D = T >For example, suppose I have these two elements in the list : 11112111 >and 1111X111 The rule will transform these two strings into one : 1111U111 After all the possible transformations (always using two strings with >only one different character and resulting another string) I will obtain >a reduced set of strings. How can I do that with mathematica?? I guess the first step is a function to identify is two strings are >different by just one ccharacter. >A loop then search in the set for any ocurrences of that and apply all >possible transformations until we can't get any redution. >Edson edsferr@uol.com.br > > -- PGP Key ID: 0x235FDED1 Please avoid sending me Word or PowerPoint attachments. http://www.gnu.org/philosophy/no-word-attachments.html ==== Subject: Set of strings reducing problem I have a problem that I haven't got any clue to solve with Mathematica. Let's say a have a list of n equal length strings: L={11111111, 11112111, 1111X111, ... ... ... 21122211} The characters used in strings are only 1, X, 2, U, M, D and T. What I want is a reduced set of strings (with all the resulting strings with the same length as all the original ones). The rule to join two strings is the following: If one string is different from the other by just one character then take the characters that are different and apply the rule bellow: 1 + X = D 1 + 2 = M 1 + U = T X + 2 = U X + M = T 2 + D = T For example, suppose I have these two elements in the list : 11112111 and 1111X111 The rule will transform these two strings into one : 1111U111 After all the possible transformations (always using two strings with only one different character and resulting another string) I will obtain a reduced set of strings. How can I do that with mathematica?? I guess the first step is a function to identify is two strings are different by just one ccharacter. A loop then search in the set for any ocurrences of that and apply all possible transformations until we can't get any redution. Edson edsferr@uol.com.br ==== Subject: Re: Set of strings reducing problem compareString compares two strings and generates the desired string if there is a single mismatch. Otherwise it generates {}. In[1]:= compareStrings[a_String,a_String]:={}; compareStrings[str1_String,str2_String]/;Equal[Length[str1],Length[str2]]:= Module[{pos,chars1,chars2,thr,rules}, rules={{1,X}|{X,1}:>D, {1,2}|{2,1} :> M,{1,U}|{U,1}|{M,X}|{X,M}|{2,D}| {D,2}:> T,{2,X}|{X,2}:>U}; chars1=Characters[str1]; chars2=Characters[str2]; thr=Thread[{chars1,chars2}]; pos=Position[thr,{a_,b_}/;b=!=a,{1},2]; If[ Equal[Length[pos],2],{}, StringJoin[ReplacePart[chars1,Extract[thr,First[pos]]/.rules,pos]]]]; Note the last argument of Position. There is no need to search beyond the second mismatch. In[3]:= compareStrings[11112111,1111X111] Out[3]= 1111U111 compareStrings[11112111,1111X121] Out[4]= {} It wasn't exactly clear what you meant by taking all transformations. If you want to look at all possible pairs of the original strings, generating a new string from a pair only if there is a single mismatch, and then looking at the collection of all the generated strings, you can: In[12]:= genStrings[a_List]:=Flatten[compareStrings[#[[1]],#[[2]]]&/@Subsets[a,{2}]]; In[14]:= origList={11112111,1111X111,11111111,11112111,21122211, D1122211}; In[15]:= genStrings[origList] Out[15]= {1111U111,1111M111,1111D111,1111U111,1111M111,T1122211} ==== Subject: Re: Set of strings reducing problem I have a problem that I haven't got any clue to solve with Mathematica. Let's say a have a list of n equal length strings: L={11111111, > 11112111, > 1111X111, > ... > ... > ... > 21122211} The characters used in strings are only 1, X, 2, U, M, D and > T. What I want is a reduced set of strings (with all the resulting strings > with the same length as all the original ones). The rule to join two strings is the following: If one string is different from the other by just one character then > take the characters that are different and apply the rule bellow: 1 + X = D > 1 + 2 = M > 1 + U = T > X + 2 = U > X + M = T > 2 + D = T > For example, suppose I have these two elements in the list : 11112111 > and 1111X111 The rule will transform these two strings into one : 1111U111 After all the possible transformations (always using two strings with > only one different character and resulting another string) I will obtain > a reduced set of strings. How can I do that with mathematica?? I guess the first step is a function to identify is two strings are > different by just one ccharacter. > A loop then search in the set for any ocurrences of that and apply all > possible transformations until we can't get any redution. > Edson edsferr@uol.com.br > Although you could stick with a string-based formulation, I would convert your strings into lists of integers, converting back to strings at the end if desired. You could do this with Map[f,Characters[string]] where f would be defined: f[1]=1; f[2]=2; f[M]=3; etc. Now you can encode your rules as a set of definitions for another function g: SetAttributes[g, Listable]; g[1,1]=1; g[2,2]=2; g[1,2]=3; etc. Because g gas Listable attribute, it will operate on two lists applying your rule element by element. Given a list of lists L of this type it would be possible to combine them in every possible way using g with code such as: Flatten[Outer[gg,L]]/.gg ->g]//Union Note the use of gg, an undefined function, to let us flatten the structure without flattening the inermost lists (there are obviously other ways to achieve this). You could use FixedPoint to repeat this process until no further change took place. David Bailey http://www.dbaileyconsultancy.co.uk ==== Subject: GroebnerBasis (was Re: Documentation) >I guess I have been rather slow but I am gradually coming round to >>the view that Mathematica's documentation leaves something to be >>desired. For example: >>?CoefficientDomain >>CoefficientDomain is an option for GroebnerBasis and >>PolynomialReduce. Valid >>choices are Integers, Rationals, RationalFunctions, or Polynomials[var]. >>However: >>?InexactNumbers >>InexactNumbers is a setting for the CoefficientDomain option of >>GroebnerBasis >>and PolynomialReduce. >>InexactNumbers are mentioned in the main documentation: >> Possible settings for CoefficientDomain are InexactNumbers, >>Rationals, RationalFunctions and Polynomials[x]. >>but this is still quite hopeless as documentation as there is nothing >>to tell the user that the proper usage is: >>GroebnerBasis[polys, vars, CoefficientDomain -> InexactNumbers[n]] >>where n is precision. > Actually CoefficientDomain->InexactNumbers should work, using a default > of 100 digits (I think). The fact that InexactNumbers[n] is undocumented > is an artifact of development history. GroebnerBasis with inexact > numbers was developed for version 3, and the use of specified precision > came in version 4 as part of NSolve overhaul. I'll forward this to the documentation folks to see if it can be updated. >No wonder that I am yet to meet a person that has heard of numerical >>Groebner basis in Mathematica never mind anyone actually using it. > Presumably you mean non-WRI employees. It gets used here a bit. I have notized that in Mathematica 3.0 and Mathematica 5.0, GroebnerBasis sometimes gives different results. Where 3.0, when eliminating, gives a polynomial with very large coefficients, 5.0 gives the empty set, although that's incorrect, probably due to numerical cancellation, quite unexpectedly. As an example, in Mathematica 5.0 GroebnerBasis[{x - y, x - 1.01 y}, {y},{x}] gives {1. y} while GroebnerBasis[{x - y, x - 1.001 y}, {y},{x}] gives {}. The correct Groebner Basis, after elimination of x is of course {y}. Likewise GroebnerBasis[{x - y, x - 1.01 y}, {x, y}] gives the correct {1. y, x} while GroebnerBasis[{x - y, x - 1.01 y}, {x, y}] gives the incorrect {x - 1.001 y} Though, if you add CoefficientDomain->Rationals, things works out quite a bit better. (Default seems to be CoefficientDomain->InexactNumbers). Should really cancellation errors occur at this level of accurancy? I agree with Andrzej that the different CoefficientDomains should be more documented, and so the risk of inaccuracy in the default setting. Stefan Karlsson Sk.9avde University, Sweden ==== Subject: Re: Set of strings reducing problem I guess I have a basic question on your transformations. You give six > rules for how to add the differing characters; are these the only > combinations possible? Yes! Those are the only possible combinations! >For example, will the combination of strings of > 1111M111 > + 11112111 > ever be encountered? Never! >In addition, when you say that the strings will > differ from each other by only one character, does this mean in _one_ > given position? Yes! The two strings that will be joined have only one different charac= ter at same position! >For example, a transpose of characters as in > 21111111 vs. 11111112 might be seen as two strings > with only one differing character, or two strings of _two_ differing > characters (one at either end). You can't join those examples! until nothing changes, my guess is that, yes, you've given the complete= > set of transformation rules. Yes! The rules are only those I gave! >In this case, what happens if I try to > decompose (someone more familiar with string manipulations, and, > perhaps, crytanalysis or group theory, may be better suited to ask thes= e > questions) the operations? Can I, for example, do something like 1 + X = D > ==>1 + D = 1+(1+X) > =(1+1) +X > =1 + X <--- Here I've assumed that because you want > strings differing by characters, that (1+1) -> 1. > In other words, is the addition associative? I think the answer to > this hinges on the answer as to the only one character problem. Yes! they are associative AND comutative! But all possible cases were men= tioned on the rules! > Interesting problem! I'm sorry I can't help > (yet) on the implementation. > C.O. I have a problem that I haven't got any clue to solve with Mathematica= . Let's say a have a list of n equal length strings: L={11111111, > 11112111, > 1111X111, > ... > ... > ... > 21122211} The characters used in strings are only 1, X, 2, U, M, D a= nd >T. What I want is a reduced set of strings (with all the resulting string= s >with the same length as all the original ones). The rule to join two strings is the following: If one string is different from the other by just one character then >take the characters that are different and apply the rule bellow: 1 + X = D >1 + 2 = M >1 + U = T >X + 2 = U >X + M = T >2 + D = T >For example, suppose I have these two elements in the list : 11112111= >and 1111X111 The rule will transform these two strings into one : 1111U111 After all the possible transformations (always using two strings with = >only one different character and resulting another string) I will obta= in >a reduced set of strings. How can I do that with mathematica?? I guess the first step is a function to identify is two strings are >different by just one ccharacter. >A loop then search in the set for any ocurrences of that and apply all= >possible transformations until we can't get any redution. >Edson edsferr@uol.com.br > ==== Subject: Re: combinations problem > hi > at first here is my program > << DiscreteMath`Combinatorica` > lst1 = Table[i, {i, 100}] > Do[ > lst2 = Table[Random[Integer, {1, 300}], {i, 100}]; > lst3 = Transpose[{lst1, lst2}]; > a = KSubsets[lst3, 2]; > num = 0; Do[ > If[ > Abs(a[[i]][[1]][[2]] - a[[i]][[1]][[1]]) == > Abs(a[[i]][[2]][[2]] - a[[i]][[2]][[1]]) || (a[[i]][[1]] > [[2]] + > a[[i]][[1]][[1]]) == (a[[i]][[2]][[2]] + a[[i]][[2]] > [[1]]), > num++;] > , {i, 1, 4950}]; > If[num > 40, Print[num]; Print[lst2]]; > , {j, 1, 2000}] lst3 will have each element in lst2 together with its ordering > such as : > Take[lst3, {1, 3}] > {{1, 48}, {2, 295}, {3, 74}} > if we take all the possible combinations in lst3 taken in pairs as in > a = KSubsets[lst3, 2] > one sublist may be for a[[1]] > {{1, 6}, {29, 34}} > the length of a will be 4950 which is number of possible > combinations for 100 elements taken in pairs. > the mission is to count the sublists such as > {{1, 6}, {29, 34}} in which abs[1-6]=abs[29-34] > or the sublists such as > {{1, 206}, {35, 172}} > in wich 1+206=35+172 > and what is the biggest possible number for such sublists when we > supply our random or carefully designed lst2 ? > in the program above we want to display only the lst2 wich will give > num>40 > how could i make my program more speedy and more efficient? > Anton First version: < 40, Sow[lst[[All,2]]]]; , {n} ];]][[1,1]] ] In[14]:= Timing[fl1 = findlists[20,300,100,40];] Out[14]= {3.25551 Second, Null} So this would take around 5 minutes to do 2000 trials. Second version uses Compile. This took a bit of work to satisfy the petty whims of the Compile deities. findlistsC = Compile[{{n,_Integer},{max,_Integer},{len,_Integer}}, Module[{lst, aaa, num=0, la=Round[len*(len-1)/2.], incr, j, k, tt}, aaa = Table[{{0,0},{0,0}}, {la}]; Table[ incr = 0; lst = Table[{s,Random[Integer,{1,max}]}, {s,len}]; For [j=1, j<=len-1, j++, For [k=j+1, k<=len, k++, incr++; aaa[[incr]] = {lst[[j]],lst[[k]]}]]; num = 0; Do [If[aaa[[j,1,1]]+aaa[[j,1,2]]==aaa[[j,2,1]]+aaa[[j,2,2]] || Abs[aaa[[j,1,1]]-aaa[[j,1,2]]]==Abs[aaa[[j,2,1]]-aaa[[j,2,2]]], num++] , {j,Length[aaa]} ]; Prepend[lst[[All,2]],num] , {n} ] ]] findlists2[n_, max_, len_, threshhold_] := Map[Rest,Select[findlistsC[n,max,len], (First[#]>=threshhold)&]] In[18]:= Timing[fl2 = findlists2[200,300,100,40];] Out[18]= {3.50047 Second, Null} So this is about 10 times faster. Daniel Lichtblau Wolfram Research ==== Subject: combinations problem hi at first here is my program << DiscreteMath`Combinatorica` lst1 = Table[i, {i, 100}] Do[ lst2 = Table[Random[Integer, {1, 300}], {i, 100}]; lst3 = Transpose[{lst1, lst2}]; a = KSubsets[lst3, 2]; num = 0; Do[ If[ Abs(a[[i]][[1]][[2]] - a[[i]][[1]][[1]]) == Abs(a[[i]][[2]][[2]] - a[[i]][[2]][[1]]) || (a[[i]][[1]] [[2]] + a[[i]][[1]][[1]]) == (a[[i]][[2]][[2]] + a[[i]][[2]] [[1]]), num++;] , {i, 1, 4950}]; If[num > 40, Print[num]; Print[lst2]]; , {j, 1, 2000}] lst3 will have each element in lst2 together with its ordering such as : Take[lst3, {1, 3}] {{1, 48}, {2, 295}, {3, 74}} if we take all the possible combinations in lst3 taken in pairs as in a = KSubsets[lst3, 2] one sublist may be for a[[1]] {{1, 6}, {29, 34}} the length of a will be 4950 which is number of possible combinations for 100 elements taken in pairs. the mission is to count the sublists such as {{1, 6}, {29, 34}} in which abs[1-6]=abs[29-34] or the sublists such as {{1, 206}, {35, 172}} in wich 1+206=35+172 and what is the biggest possible number for such sublists when we supply our random or carefully designed lst2 ? in the program above we want to display only the lst2 wich will give num>40 how could i make my program more speedy and more efficient? Anton ==== Subject: Install Issue with Ver 5.2 WinXp Not sure on this, so this is just some feedback on installation. I'm not too thrilled with the installation of this program. One issue.I have the this program, you may want to remove this beta program prior to installation of 5.2. (5.2 has Indexer). Go to Windows Control Panel, Add/Remove Programs, and remove the program Wolfram Notebook Indexer Beta. Again, just feedback if it will help. :>) Dana ==== Subject: Re: Set of strings reducing problem >I have a problem that I haven't got any clue to solve with >Mathematica. >Let's say a have a list of n equal length strings: >L={11111111, > 11112111, > 1111X111, > ... > ... > ... > 21122211} >The characters used in strings are only 1, X, 2, U, M, >D and T. >What I want is a reduced set of strings (with all the resulting >strings with the same length as all the original ones). >The rule to join two strings is the following: >If one string is different from the other by just one character >then take the characters that are different and apply the rule >bellow: >1 + X = D >1 + 2 = M >1 + U = T >X + 2 = U >X + M = T >2 + D = T >For example, suppose I have these two elements in the list : >11112111 and 1111X111 >The rule will transform these two strings into one : 1111U111 The function below will add two strings per your rules above addString[x_, y_] := Block[{X, M, T, D, U}, StringJoin @@ ToString /@ ((ToExpression /@Characters[x] + ToExpression /@ Characters[y] /. {D + 2 -> 2*T, M + X -> 2*T, X + 2 -> 2*U, U + 1 -> 2*T, 3 -> 2*M, X + 1 -> 2*D})/2)] What I've done is use ToExpression to convert each string to a list of symbols and integers then use Mathematica's pattern matching to implement your rules. The final result is then converted back to a string. I've used Block to ensure the results are not affected by usage of X, M etc as variables elsewhere in the notebook. >After all the possible transformations (always using two strings >with only one different character and resulting another string) I >will obtain a reduced set of strings. >How can I do that with mathematica?? >I guess the first step is a function to identify is two strings are >different by just one ccharacter. A loop then search in the set for >any ocurrences of that and apply all possible transformations until >we can't get any redution. The following function will count the number of differences in two strings of equal length noDifferent[x_, y_] := Count[ MapThread[SameQ, {Characters[x], Characters[y]}], False] I can generate a list of all pairs that differ by a single character as follows: Union[Pick[Flatten[Outer[Sort@{#1, #2}& , L, L], 1], (#1 == 1 & ) /@ Flatten[Outer[noDifferent, L, L]]]] and finally doing addString@@@list where list is the list of pairs that differ by one character will generate the reduced list you want. -- To reply via email subtract one hundred and four ==== Subject: Re: Can't assign value to symbols Andrzej Your reply has led me to the following code, which improves an earlier posting. Your idea to use strings led to doing so for T-row #1. Other than T[[1]] not being the symbols list {a,b,c}, do you see a problem with the code below? Bruce a = b = c = 2; T = {{a, b, c}, {3, 4, 2}, {12, 2, 1}, {6, 5, -3}}; Scan[Clear, T[[1]]]; Do[MapThread[Set, {Y = ToExpression@T[[1]], T[[k]]}]; Print[a + b + c, , Total@Y]; Scan[Clear, T[[1]]], {k, 2, Length@T}]; ==== Subject: Can't assign value to symbols Bruce The only way I can imagine of dealing with this situation is if you first make sure that the data that are originally imported into Mathematica are imported as a string and not an expression. (This is easy to do with external data with Mathematica functions ReadList or Import ). So, for example, if to start with T has the form T = {{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; Then all you need to do is: Scan[Clear, StringCases[T, LetterCharacter]] and all the prior values of a, b, and c will b cleared. You can then convert T to Expression with ToExpression. Andrzej > Andrzej > I've moved your Scan to the inside-end of the loop, since it kills > two birds with one stone. The code below works as intended unless > a,b,c have values (and then error messages abound). The only way I know to avoid this problem is to make Scan > [Clear,Names[Global`*]] the new first line...except that this > (unfortunately) zaps ALL variables. There must be a better way (than my above idea) to clear the T[[1]]- > variables (that may have values coming into the code below). How would you tell Mathematica to hold unevaluated the T[[1]]- > elements so that they can be used to clear the associated variables? Bruce T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; > X=Map[SymbolName,T[[1]]]; Do[Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}]; > Scan[Clear,X], > {k,2,Length@T}]; ==== > Subject: > Can't assign value to symbols I see. In this case, does not Scan[Clear,X] after the Do loop do what you want? Andrzej > Andrzej >> The aim is to explictly avoid (throughout the code) referring to >> the variables. >> For instance, suppose matrix T (in my code) springs from an Excel >> spreadsheet imported into Mathematica, one whose top row declares >> the variable names, and whose later rows give the variables' values >> for each iteration in a simulation. Suppose further that the >> number of columns may change. >> Bruce >> Bruce >> ==== >> Subject: Can't >> assign value to symbols >> To tell the truth, I don't understand what you are trying to do. Why >> do you want to reevaluate the line X=... ? >> If you do not want at the end of your Do loop the variables a,b,c to >> have assigned values the simplest way is to use Block: >> Block[{a, b, c}, Do[ >> Print@MapThread[Set, {ToExpression[X, >> StandardForm, Unevaluated], T[[k]]}], >> {k, 2, Length@T}]]; >> All the assignments will now take place locally inside Block and the >> variables {a,b,c} are automatically cleared on exit-ting Block. >> Andrzej Kozlowski > Andrzej Using your reply, I offer this code for Lee's consideration. Although it works when first run, if re-run without quitting the > kernal, the X= line causes an error because a,b,c are > instantiated. At the start of the program, how can I clear these variables > WITHOUT explicitly declaring them? I've failed to find the right > argument for Clear (e.g., Clear@Subscript...can't find the right > counterpart for Subscript). > Bruce > T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; > Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}], > {k,2,Length@T}]; > ==== > Subject: Can't > assign value > to symbols Instead of ToExpression use ToExpression[#, StandardForm, Unevaluated] & For example: > In[1]:= > parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; In[2]:= > MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] > & /@ > parameters[[1]], > parameters[[3]]}] ; In[3]:= > a Out[3]= > 4 In[4]:= > MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] > & /@ > parameters[[1]], > parameters[[2]]}] ; In[5]:= > a Out[5]= > 1 > Of course instead of Standardform you can use TraditionalForm or > even > InputForm. Andrzej Kozlowski Chiba, Japan >> but >> now realize there is one additional problem. Once I've executed >> the >> statement MapThread[(#1 = #2) & , {parameters[[1]], parameters >> [[3]]}] , >> because I've specified the first row of parameters as symbols, >> this >> row now loses the symbol names and takes on the assigned values, >> such >> that my attempt to execute the MapThread Statement again for a >> different >> set of values, e.g. parameters[[2]], fails. >> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; >> in: {a,b,c} >> Out: {4,5,6} >> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; >> out: Set:: setraw : Cannot assign to raw object 4 ... etc >> I want to preserve the first row of parameters as the symbol >> names, so >> one solution is to use strings, and modify David Park's solution to >> turn >> the strings into symbol names when assigned, This works the first >> time >> it is run, and parameters[[1]] still contains the strings, but ut >> still >> fails the second time. but why? >> in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, >> 9}}; >> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >> parameters[[3]]}] ; >> in: {a,b,c} >> out {4,5,6} >> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >> parameters[[3]]}] ; >> out: Set:: setraw : Cannot assign to raw object 4 ... etc >> why does the MapThread statement fail the second time? It seems >> ToExpression/@parameters[[1]] converts the strings a, b, c to >> symbols but then evaluates them prior to returning them, so the >> attempt >> to assign to them in MapThread fails. >> How can I modify the MapThread statement so I can call it multiple >> times, each time assigning values to the symbols identified by the >> strings in the first row?? >> Lee >>> Lee, >>>> Set (=) has a higher precedence Than Function (&). So all you have >>> to do is >>> add parentheses. >>>> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >>>> MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >>> {4, 5, 6} >>>> David Park >>> djmp@earthlink.net >>> http://home.earthlink.net/~djmp/ >>>>>>>>>>>> Situation: I have a table that contains parameters and sets of >>> values >>> that I want to assign to the parameters for the purposes of >>> running a >>> simulation. >>> - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >>> - want to assign values in a given row to the symbols listed in >>> the >>> first row. >>> - tried using: MapThread[ #1 = #2 &, {parameters[[1]], >>> parameters >>> [[3]]} ] >>> - fails with error Tag Slot in #1 is Protected >>> - tried adding Unprotect[#1] and a variety of other attemps, but >>> can't >>> get it to work. >>>> Anyone know how might accomplish this? >>>> Lee >>>>>>>>> > ==== Subject: Problem with multiple function calling from a novice... Hi all. I have a small problem, and I would like some help. Suppose I define a few functions, say a,b,c etc. Question 1: How can I call them from inside a program, so that they ALL show what they evaluate in turn? (Not just the last one, but each one.) Question 2: How can I modify an If...Then clause of the form: If[ , a;b;c;...etc.] or a While loop: While[ , a;b;c;...etc.] so that, again, they ALL show what they evaluate in turn? (Again, not just the last one like in this case). Sami