wolfram-mathematica

Question on “smart” replacing in mathematica

柔情痞子 提交于 2019-11-30 07:29:20
How do I tell mathematica to do this replacement smartly? (or how do I get smarter at telling mathematica to do what i want) expr = b + c d + ec + 2 a; expr /. a + b :> 1 Out = 2 a + b + c d + ec I expect the answer to be a + cd + ec + 1 . And before someone suggests, I don't want to do a :> 1 - b , because for aesthetic purposes, I'd like to have both a and b in my equation as long as the a+b = 1 simplification cannot be made. In addition, how do I get it to replace all instances of 1-b , -b+1 or -1+b , b-1 with a or -a respectively and vice versa? Here's an example for this part: expr = b +

Seeing truncated messages in Mathematica

感情迁移 提交于 2019-11-30 07:29:12
问题 Is it possible to see full version of a Message that got truncated? IE, I see something along the lines of 0.105309,0.394682,<<20>>,<<20>>,<<20>>,0.394631 in the Messages window. I'm guessing <<20>> represents omitted parts, how do I get the whole thing? The function called is FindMaximum on a problem with 50 variables. Update: Simon's answer seems to work for general messages, also I found an approach that's specific to capturing the FindMaximum "not a real number" message. To get the point

Memory use of Apply vs Map. Virtual memory use and lock-ups

坚强是说给别人听的谎言 提交于 2019-11-30 07:09:37
I needed to find the sum of all pairs of numbers in a long list of pairs. Lots of ways to do this in Mathematica, but I was thinking of using either Plus or Total . Since Total works on lists, Map is the functional programming instrument to use there and Apply at level 1 (@@@) is the one to use for Plus , as Plus takes the numbers to be added as arguments. Here is some demo code ( warning : save all your work before executing this!): pairs = Tuples[Range[6000], {2}]; (* toy example *) TimeConstrained[Plus @@@ pairs; // Timing, 30] (* Out[4]= {21.73, Null} *) Total /@ pairs; // Timing (* Out[5]

How do I stop recursion happening in a Format/Interpretation Mathematica construction?

折月煮酒 提交于 2019-11-30 07:02:19
This question follows on from the answer given by Michael Pilat in Preventing “Plus” from rearranging things . There he defined a custom + notation using Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]] The problem with this is you can't copy and paste the output (although % or Out[] still works). To get around this you should use the Interpretation type facility which allows an expression to be displayed as one thing, but interpreted as another when supplied as input. My modification of Michael's answer is Format[myPlus[expr__]] := Interpretation[Row[{expr}, "+"], myPlus[expr]] This can be

Find the largest rectangular block satisfying some condition without iterating explicitly

懵懂的女人 提交于 2019-11-30 07:02:16
I have a few large 2D arrays like: 1 2 3 4 5 -------------- 1 | 0 1 1 1 0 2 | 0 1 1 1 0 3 | 0 1 0 1 1 4 | 0 1 0 1 1 So, the largest rectangular block (by area) satisfying ==1 starts at (1,2) and its dimensions are (2,3). How to find it with Mathematica without iterating explicitly? NB: Just to ease your testing here is one of my samples: matrix = ImageData@Binarize@Import@"http://i.stack.imgur.com/ux7tA.png" This is my attempt using BitAnd maxBlock[mat_] := Block[{table, maxSeq, pos}, maxSeq[list_] := Max[Length[#] & /@ Append[Cases[Split[list], {1 ..}], {}]]; table = Flatten[Table[ MapIndexed

Mathematica running out of memory

女生的网名这么多〃 提交于 2019-11-30 06:55:47
问题 I'm trying to run the following program, which calculates roots of polynomials of degree up to d with coefficients only +1 or -1, and then store it into files. d = 20; n = 18000; f[z_, i_] := Sum[(2 Mod[Floor[(i - 1)/2^k], 2] - 1) z^(d - k), {k, 0, d}]; Here f[z,i] gives a polynomial in z with plus or minus signs counting in binary. Say d=2, we would have f[z,1] = -z 2 - z - 1 f[z,2] = -z 2 - z + 1 f[z,3] = -z 2 + z - 1 f[z,4] = -z 2 + z + 1 DistributeDefinitions[d, n, f] ParallelDo[ Do[ root

Using nested slots (#)

不问归期 提交于 2019-11-30 06:55:34
Suppose I want to construct something like Array[#1^#2 == 3 &, {3, 3}] And now I want to replace the "3" with a variable. I can do, for example: f[x_] := Array[#1^#2 == x &, {x, x}] The question is: Is there a way using only slots and & as the functional notation? How about Map[Last, #] & /@ Array[#1^#2 == #3 &, {#, #, #}] &[3] Horrendously ugly element extraction, and very interestingly Map[Last, #]& gives me a different result than Last /@ . Is this due to the fact that Map has different attributes than & ? Leonid Shifrin Not really the answer to the original question, but I noticed that

Why does Default behave like this?

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:47:19
One may set a Default value for the arguments of a function: Default[f] = 5; And then use: f[a_, b_.] := {a, b} f[1, 2] f[1] {1, 2} {1, 5} This creates the following Values: DefaultValues[f] DownValues[f] {HoldPattern[Default[f]] :> 5} {HoldPattern[f[a_, b_.]] :> {a, b}} From this one might think that the value 5 is not fixed in the definition of f , but addresses the DefaultValues assignment. However, if we change the DefaultValues , either directly or using: Default[f] = 9; DefaultValues[f] {HoldPattern[Default[f]] :> 9} and use f again: f[1] {1, 5} we see that the new value is not used.

code manipulation via interactive tree for Mathematica

丶灬走出姿态 提交于 2019-11-30 06:47:14
问题 This question caused me to ponder an interactive method for editing code. I wonder if it is possible to implement something like this given the dynamic capabilities of Mathematica. Consider an expression: Text[Row[{PaddedForm[currentTime, {6, 3}, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}]}]] And its TreeForm : I would like to be able to edit that tree directly, and then have the result translated back into Mathematica code. One should at least be able to: rename nodes, replacing

Multigraphs in Mathematica 8

对着背影说爱祢 提交于 2019-11-30 06:45:19
I just spent a couple of hours trying to convert some old code that uses Mathematica 7's GraphPlot to use the new Mathematica 8 Graph functions. It seemed sensible since the new graph drawing is much nicer and it has things like AdjacencyMatrix and KirchhoffMatrix built in. The problem is that I can not figure out how to get graphs with multiple edges to work in Mma 8. The Feynman graph that I use as my canonical example is the two-loop vacuum graph GraphPlot[{1 -> 2, 1 -> 2, 1 -> 2}, MultiedgeStyle -> .5, DirectedEdges -> True, VertexCoordinateRules -> {{-1, 0}, {1, 0}}] Trying to make the