A66 ==== Subject: Sequential Quadratic Programming I have a question about sequential quadratic programming algorithm. Does anyone know about free packages for Mathematica implementing the SQP algorithm? Any hints would be greatly appreciated Marco Gabiccini ==== Subject: Re: Re: ListInterpolation res = ListInterpolation[ Transpose[{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10 , 11 , 12}}], {{1, 1.3, 2, 5}, {0, 1.5, 2}}]; I get res[1, 0.5] = 1. whereas for me the correct answer would be res[1, 0.5] = 5/3 since {1, 1.3, 2, 5} is the x coordinate of the data table {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10 , 11 , 12}} and {0, 1.5, 2} the y coordinates. Where does I make a mistake? I have a problem with the ListInterpolation instruction. I would like > to specify some explicit lists of positions for grid lines like this: res == ListInterpolation[{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{1, 1.3, 2. 5, > 5}, {0.5, 1.3}}] But it does not work: when I try to calculate res[2, 1.1], I get > ListInterpolation[{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{1, 1.3, 2.5, 5}, > {0.5, 1.3}}][2, 1.1] I thank you in advance for your help. Yours sincerely Stephane Fay > Paris Observatory > France Your input is not in proper format (and use = for assignments, not ==): In[1]:= res = ListInterpolation[ > Transpose[{{1, 2, 3, 4}, {5, 6, 7, 8}}], > {{1, 1.3, 2.5, 5}, {0.5, 1.3}}]; > ListInterpolation::inhr: > Requested order is too high; order has been reduced to {3, 1}. More... In[2]:= res[2, 1.1] > Out[2]= > 6.031081081081081 > -- > Peter Pein > Berlin ==== Subject: Re: More help on sorting out a large list We can solve your problem by using Split to split the rows into equal rows and then we replace every batch of equal rows by an avarage. The split command will look like (assuming your data-matrix is in data, and x is the 5., y the 6. row): tmp= Split[data,#1[[{5,6}]]==#2[[{5,6}]]&] To get the avarage, we need a function that gives the indices of the rows to take the avarage from. That is, for an even number: {n/2,n/2+1} for an odd number:{(1+n)/2,(1+n)/2} f1[n_]:= If[EvenQ[n],{n/2,n/2+1},{(n+1)/2,(n+1)/2}]; with this we finally get: Mean[ Take[#,f1[Length[#]]]]& /@ tmp sincerely, Daniel > Ok here's a problem I've been trying to figure out for awhile. I have a large XYZ sorted matrix and what i need is to look at two columns x,y and and if it is a unique point keep the row, if there are multiple rows with same x,y pair i need to remove all of them and leave only the middle Z row or the average of the middle two Z rows for example x y z > {1, 6, 4, 8, 4, 5, 8, 3} drop > {1, 3, 5, 8, 4, 5, 3, 3}--take > {5, 3, 6, 8, 4, 5, 6, 3} drop > {4, 5, 3, 4, 5, 6, 1, 0}drop > {2, 5, 4, 3, 5, 6, 9, 1} > {1, 2, 5, 4, 5, 6, 7, 8} -take average > {3, 5, 6, 3, 5, 6, 6, 4}drop > {5, 3, 5, 2, 7, 8, 2, 5}----take > ==== Subject: Re: More help on sorting out a large list try Split[data, #1[[5]] == #2[[5]] && #1[[6]] == #2[[6]] &] to group the data. But your posting is unclear what take the average mean. Is it the average over all columns ? Jens tornado78 schrieb im > Ok here's a problem I've been trying to figure > out for awhile. I have a large XYZ sorted > matrix and what i need is to look at two columns > x,y and and if it is a unique point keep the > row, if there are multiple rows with same x,y > pair i need to remove all of them and leave only > the middle Z row or the average of the middle > two Z rows for example x y z > {1, 6, 4, 8, 4, 5, 8, 3} drop > {1, 3, 5, 8, 4, 5, 3, 3}--take > {5, 3, 6, 8, 4, 5, 6, 3} drop > {4, 5, 3, 4, 5, 6, 1, 0}drop > {2, 5, 4, 3, 5, 6, 9, 1} > {1, 2, 5, 4, 5, 6, 7, 8} -take average > {3, 5, 6, 3, 5, 6, 6, 4}drop > {5, 3, 5, 2, 7, 8, 2, 5}----take > ==== Subject: Weird behavior of SparseArray I don't understand what is going on with SparseArray If I write this: cp1 = SparseArray[{i_, j_} -> 0, {4, 2}]; cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; cp1 // MatrixForm I get cp1 with all entries equal to zero However, if use Table instead of SparseArray I get the right answer. More surprisingly, if I write this: cp1 = SparseArray[{i_, j_} -> 0, {4, 2}]; cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; cp1 // MatrixForm cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; cp1 // MatrixForm I get ones in the first column!! Any explanation? -- Adel ==== Subject: Re: Weird behavior of SparseArray $Version Hi Adel, in this version it seems to work o.k. On the other hand, I saw problems with sparse matrixes in numeric routines, but I did not report them. sincerely, Daniel I don't understand what is going on with SparseArray If I write this: cp1 = SparseArray[{i_, j_} -> 0, {4, 2}]; > cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; > cp1 // MatrixForm I get cp1 with all entries equal to zero However, if use Table instead of SparseArray I get the right answer. More surprisingly, if I write this: cp1 = SparseArray[{i_, j_} -> 0, {4, 2}]; > cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; > cp1 // MatrixForm > cp1[[Range[4], 1]] = cp1[[Range[4], 1]] + {1, 1, 1, 1}; > cp1 // MatrixForm I get ones in the first column!! Any explanation? > ==== Subject: Re: (presumably) easy AspectRatio question I tried this. The effect is less pronounced, but when I run this (including *AspectRatio -> Automatic* on both Show[] commands), I see the following artifact. On the unrotated rectangle, the bottom side is about 300 pixels and horizontal. On the rotated rectangle, the bottom side is only about 100 pixels (and angled 30 degrees of course). This is very curious. I will keep experimenting with other AspectRatios. > You should make sure to set both the first and the last graphic to the > same AspectRatio. Try this: ==== Subject: Re: (presumably) easy AspectRatio question you have to adapt the AspectRatio to your drawing. This is done by the option: AspectRatio->Automatic sincerely, Daniel > I have a small program that draws a rectangle around the plot of my > data. I want to rotate my plot, so I used the Geometry`Rotations` > package. I cannot get my plot to look like a simple rotation. I have > tried lots of different AspectRatios and ImageSizes to no avail. When I run the small program below, the bounding rectangle prints > correctly, but the rotated lines are skew (almost like 3D). I am guessing this is a simple problem, but it is currently beyond me. > The relevant portion of the program is: In[1] = bL = {{0, -1.76}, {1., -1.76}, {1., 1.76}, {0, 1.76}}; > In[2] = Show[Graphics[Line[bL]]]; > In[3] = << Geometry`Rotations` > bLRot = Thread[Rotate2D[bL, N[Pi/6]]] > Out[3] = {{-0.88, -1.5242}, {-0.0139746, -2.0242}, {1.74603, 1.0242}, > {0.88, 1.5242}} > In[4] = Show[Graphics[Line[bLRot]]]; Any help is appreciated. ==== Subject: Re: (presumably) easy AspectRatio question Just use the AspectRatio option in both of your Show statements. Show[Graphics[Line[bLRot]], AspectRatio -> Automatic]; David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ I have a small program that draws a rectangle around the plot of my data. I want to rotate my plot, so I used the Geometry`Rotations` package. I cannot get my plot to look like a simple rotation. I have tried lots of different AspectRatios and ImageSizes to no avail. When I run the small program below, the bounding rectangle prints correctly, but the rotated lines are skew (almost like 3D). I am guessing this is a simple problem, but it is currently beyond me. The relevant portion of the program is: In[1] = bL = {{0, -1.76}, {1., -1.76}, {1., 1.76}, {0, 1.76}}; In[2] = Show[Graphics[Line[bL]]]; In[3] = << Geometry`Rotations` bLRot = Thread[Rotate2D[bL, N[Pi/6]]] Out[3] = {{-0.88, -1.5242}, {-0.0139746, -2.0242}, {1.74603, 1.0242}, {0.88, 1.5242}} In[4] = Show[Graphics[Line[bLRot]]]; Any help is appreciated. ==== Subject: Periodic function Roots (Style ACos(x)+BCos(2x)+...==K). Mathematica can't let me us 'Solve' function, so it gives me the error: Solve::tdep: The equations appear to involve the variables to be solved for in an essentially non-algebraic way. To arrange this, i'm making a little program involving FindRoot function to find at least two roots from the ecuation in the first period. But I would like to know if there are some other method to do this more easily. F. Bellas ==== Subject: Re: Periodic function Roots An example of what i do in such a case: In[1]:=f[x_]:=Cos[x]+.5 Cos[2x]+.25 Cos[3x]+2.5 Cos[4x] In[2]:=t=Table[FindRoot[f[x] == 0,{x,u}],{u,0.1,2Pi,0.1}]; In[3]:=Union[t,SameTest -> (Abs[(x/.#2)-(x/.#1)]<10^-5&)] Out[3]= {{x -> -0.51129},{x -> 0.51129},{x -> 1.20136}, {x -> 1.91222},{x -> 2.81887},{x -> 3.46432}, {x -> 4.37096},{x -> 5.08183},{x -> 5.7719}} v.a. ==== Subject: poly question I have a polynom called ftlmat (Dialog) In[187]:= ftlmat (Dialog) Out[187]= 2*a^2*b^2*c + 2*a*b^2*c*d - 2*b^2*c^2*d + b^2*c^2*d^2 + 4*a^2*b*c*e - 2*a^2*c^2*e + 2*a*b*c^2*e + 4*a*b*c*d*e - 4*a*c^2*d*e - 2*b*c^2*d*e - 2*c^2*d^2*e + 2*b*c^2*d^2* e + 2*a^2*c*e^2 + 2*a*c^2*e^2 + 2*a*c*d*e^2 + c^2*d^2*e^2 + 2*a^2*b*c*f - 2*a*b^2*d*f + 4*a*b*c*d*f - 4*b^2*c*d*f - 2*b^2*d^2*f + 2*b*c*d^2*f + 2*b^2*c*d^2* f - 2*a^2*c*e*f + 4*a*b*c*e*f - 4*a*b*d*e*f - 4*a*c*d*e*f - 4*b*c*d*e*f - 4*b*d^2*e*f - 2*c*d^2*e*f + 4*b*c*d^2*e*f + 4*a*c*e^2* f - 2*a*d*e^2*f - 2*d^2*e^2*f + 2*c*d^2*e^2* f + 2*a^2*b*f^2 + 4*a*b*d*f^2 - 2*b^2*d*f^2 + 2*b*d^2*f^2 + b^2*d^2*f^2 + 2*a*b*e*f^2 - 2*b*d*e*f^2 + 2*b*d^2*e*f^2 + 2*a*e^2*f^2 + d^2*e^2*f^2 If I do a PolynomialReduce of it the following way, I get: In[170]:= PolynomialReduce[ftlmat, {a*b*c, a*b*f, a*c*e, a*e*f, b*c*d, b*d*f, c*d*e, d*e*f}, {a, b, c, d, e, f}] Out[170]= {{2*a*b + 2*b*d + 4*a*e + 2*c*e + 4*d*e + 2*a*f + 4*d*f + 4*e*f, -2*b*d - 4*d*e + 2*a*f + 4*d*f + 2*e*f, -2*a*c - 4*c*d + 2*a*e + 2*c*e + 2*d*e - 2*a*f - 4*d*f + 4*e*f, -2*d*e + 2*e*f, -2*b*c + b*c*d - 2*c*e + 2*c*d*e - 4*b*f + 2*d*f + 2*b*d*f - 4*e*f + 4*d*e*f, -2*b*d - 4*d*e - 2*b*f + 2*d*f + b*d*f - 2*e*f + 2*d*e*f, -2*c*d + c*d*e - 2*d*f + 2*d*e*f, -2*d*e + d*e*f}, 0} The result show that the first poly I got - related to a*b*c - has all 6 variables, the next has 5 and the rest goes like 5,3,5,4,4,3. If I total them it is 35. My question is what series of polynomials should I use in PolymonialReduce to get results which contain the least amount of variables each - that is the total of the number of variables in each resulting polynom should be minimal, and on the same time the number of selected polynoms should be also minimal and their construction is simple - not necessary the same length as in my case - and I should not get any reminder in the result of PolynominalReduce. If I look PolynomialReduce as giving a vectorization of the polynom regarding to the selected {poly1,poly2,...} base, then the components of the result are the polynomial projections to the individual base polynoms. I would like to select a base where the resulting components have the minimum number of variables per component and I want this base to be as simple as possible, that is they also should have minimum number of variables in them. I am sure algebra has some theory for it, but my brain is just not recalling it right now. Any good tip, J.87nos ==== Subject: Mathematica 5.1.1 on Fedora Core 4? Has anyone here tried Mathematica 5.1.1 on Fedora Core 4? There are various remarks in the FC4 Release notes about thread libraries that make me wonder if it will work, so I thought that I'd ask before committing to an upgrade. -- ------------------ http://public.xdi.org/=joe.christy ------------------ == If I can save you any time, give it to me, I'll keep it with mine. == ==== Subject: Integrate and Boole problems I'm using Mathematica 5.1 and I'm getting some inconsistent results when integrating the Boole function. For example: Integrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)*((x + 1)^2 + y^2 + z^2 - 3^2) = 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}]; N[%] 50.2654821239234 + 3.829918318813164*^-9*I NIntegrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)*((x + 1)^2 + y^2 + z^2 -3^2) = 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}] 108.90854533339399 - 2.749275252793041*^-29*I Am I doing something wrong or is this just a limit of the Integrate function? ==== Subject: Re: Mathematica Visualization site - update > I have tested this under IE, Opera, Firefox, and Safari (all current > versions). I haven't seen a case where this doesn't work. The effect > you are referring to is generated by CSS. Its possible you are using a > browser that doesn't support CSS properly. What browser (and version) are you using? I just tried it under Konqueror (3.3.2) and Galeon (1.3.21) and it screws up on both. I don't think it is CSS-related as both handle CSS well on other pages. HTML is a pain... :-( -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com ==== Subject: Re: Sudoku puzzle > deficient in that there are situations in which no unique solution at > each stage is found even though there exist squares for which a unique > value is determined. Here is a simple example: . . . | 1 . . | . . . > . . . | . . . | . . . > . . . | . . . | . . . > --------------------- > . . . | . 7 . | . . . > . . . | . . . | . . . > . . . | . 9 . | . . . > --------------------- > . . . | . . . | . . . > . . . | . . . | . . . > . . . | . . 1 | . . . Just given this much information it is clear that there must be a 1 in > the {5,5} square. A short solution, which I placed on Mathsource last week, applies two basic rules before resorting to backtracking. The rules are sufficient to find the 1 at {5,5} in this example. http://library.wolfram.com/infocenter/MathSource/5690/ > The solutions by Fred Simons and Andrzej Kozlowski (previously posted to > MathGroup) involving backtracking work fine (I did not select their > solutions for TMJ because it did not show progress towards the solution) > -- but I wonder if for the sudoku puzzles whether backtracking is ever > required? I think that any general solver must implement backtracking to deal with under-specified problems, where there are multiple solutions, such as the one shown above. There is insufficent information to proceed once the {5,5} position is found, so a guess must be made for one of the remaining positions. You can only know whether that guess is consistant with solution by atempting to solve it and back tracking on a contradiction. If that wasn't true then you would have been able to proceed. I would guess that puzzles with a single unique solution can all be solved without backtracking, but the two rules that I implemented are insufficent on the more difficult problems. Jon McLoone ==== Subject: Re: Sudoku puzzle > The solutions by Fred Simons and Andrzej Kozlowski (previously posted to > MathGroup) involving backtracking work fine (I did not select their > solutions for TMJ because it did not show progress towards the solution) > -- but I wonder if for the sudoku puzzles whether backtracking is ever > required? Backtracking is not required because you can (in principle) generate a list of all possible puzzles, along with their answers. Then your code can look like: If[puzzle == puzzle1, Return[answer1]] If[puzzle == puzzle2, Return[answer2]] ... So the question should be what minimum logical complexity (under some defined metric) is required to avoid backtracking. Scott -- Scott Hemphill hemphill@alumni.caltech.edu This isn't flying. This is falling, with style. -- Buzz Lightyear ==== Subject: Re: Sudoku puzzle See, for example: http://library.wolfram.com/infocenter/MathSource/5690/ Michal > Many readers would be aware that Sudoku is a puzzle (craze) that has > recently appeared in many major newspapers around the world as a regular > puzzle. (See http://www.sudoku.com and http://www.sudokufun.com/ An open source sudoku project exists with a Java applet available at > sourceforge: http://sudoku.sourceforge.net/) Mathematica in Education and Research would like to issue a challenge to > the > Mathematica community to create a Mathematica version of Sudoku: a package > that both creates Sudoku puzzles and solves them (either totally within a > notebook or via the GUI interface). Education > and Research describing the code and the (presumed?) challenges faced and > overcome in writing the code. Michael Honeychurch > Mathematica in Education and Research > http://www.ijournals.net > ==== Subject: Re: Sudoku puzzle I thought this was a nice exercise in backtracking with Mathematica, so Then, to my surprise, in mathgroup some contributions were devoted to sudoku's. So I cannot resist the temptation to post my backtracking solution. The administration is done in a 9x9-matrix called possiblechoices. Each entry contains a list of all possible choices for that position. Hence initially the entries of this matrix are {1,2,...9}. The sudoku under construction is the 9x9-matrix result. Initially all entries are 0. Most of the work is done by the function enterchoice[{i,j,n}]. It manipulates both the matrices result and possiblechoices. The number i is the row number, j is the column number and n is the value of the entry. Of course this function may be applied only when n is an allowed choice for the entry. In that case it reduces the possibilities for the entries on the row, the column and the 3x3-block of (i,j) in the matrix possiblechoices and it adapts the matrix result. The backtracking is done in the folowing way. The entries with only one possible choice can be filled at once. Having done that, we look for an entry with a minimal number of choices, so at least 2 choices. We choose the first element and place the rest on the list stilltobedone. Then we continue in the same way until no further choices are possible. When we found a sudoku, it is placed on the list of solutions. Then we proceed with the last item on the list stilltobedone, etc, until this list is empty. sudokuForm[mat_] := DisplayForm[ GridBox[Map[GridBox, Partition[mat, {3, 3}], {2}], GridFrame -> True, RowLines -> True, ColumnLines -> True]] sudokuQ[mat_] := And @@ Flatten[ {(Union[#1] == Range[9] & ) /@ mat, (Union[#1] == Range[9] & ) /@ Transpose[mat], Map[Union @@ #1 == Range[9] & , Partition[mat, {3, 3}], {2}]}] enterchoice[{i_, j_, n_}] := Block[{}, If[MemberQ[possiblechoices[[i,j]], n], possiblechoices[[i]] = (DeleteCases[#1, n] & ) /@ possiblechoices[[ i]]; possiblechoices[[All,j]] = (DeleteCases[#1, n] & ) /@ possiblechoices[[ All,j]]; possiblechoices = MapAt[DeleteCases[#1, n] & , possiblechoices, Flatten[Outer[List, Partition[Range[9], 3][[Ceiling[i/3]]], Partition[Range[9], 3][[Ceiling[j/3]]]], 1]]; possiblechoices[[i,j]] = {}; result[[i,j]] = n, Return[possiblechoices = testarray]]] sudoku[mat_] := Block[{possiblechoices = Array[Range[9] & , {9, 9}], result = Array[0 & , {9, 9}], stilltobedone = {}, testarray = Array[{} & , {9, 9}], solutions = {}, z, pos}, z = With[{z = Position[mat, _Integer? Positive]}, Flatten /@ Transpose[ {z, Extract[mat, z]}]]; Scan[enterchoice, z]; While[possiblechoices != testarray || stilltobedone != {}, z = Min[Map[Length, possiblechoices, {2}] /. 0 -> 10]; Which[z == 1, z = With[{z = Position[possiblechoices, {_}]}, Apply[Join, Transpose[ {z, Extract[possiblechoices, z]}], {1}]]; Scan[enterchoice, z], z < 10, With[{pos = Position[possiblechoices, _?(Length[#1] == z & )][[1]]}, AppendTo[stilltobedone, ReplacePart[ possiblechoices, Rest[ (possiblechoices[[##1]] & ) @@ pos], pos]]; possiblechoices = ReplacePart[ possiblechoices, Take[ (possiblechoices[[##1]] & ) @@ pos, 1], pos]], True, possiblechoices = stilltobedone[[-1]]; stilltobedone = Most[stilltobedone]]; If[sudokuQ[result], AppendTo[solutions, result]]]; solutions] Fred Simons Eindhoven University of Technology ==== Subject: Square-root Kalman Information filter Has anyone written Mathematica code for the square-root Kalman information filter or references to its implementation? --V. Stokes