wolfram-mathematica

Uses for MapAll ( //@ )

爷,独闯天下 提交于 2019-11-29 18:58:15
问题 The function MapAll was seen as important enough to warrant the short form //@ , yet I rarely use it, especially compared to others like /@ , /. and @@@ which I use almost everywhere. What applications best leverage MapAll ? Is it used mostly in certain fields or programming styles? How often can it be used compared to other operators? 回答1: //@ is a "post-order tree traversal". It visits every node in a tree structure, each node's children being visited before the node itself. The supplied

Question on Definition and values of symbols

自古美人都是妖i 提交于 2019-11-29 18:56:05
问题 Definition "knows" the way how a value for a symbol was defined: using Set or SetDelayed . But how? As I understand, after a value for a symbol was assigned there is no any difference for the evaluator how it was assigned: by using Set or SetDelayed . It can be illustrated by the function OwnValues which always returns definitions with the Head RuleDelayed . How Definiton obtains this information? In[1]:= a=5;b:=5; Definition[a] Definition[b] OwnValues[a] Out[2]= a=5 Out[3]= b:=5 Out[4]=

Bug in Mathematica: regular expression applied to very long string

不羁的心 提交于 2019-11-29 18:07:21
问题 In the following code, if the string s is appended to be something like 10 or 20 thousand characters, the Mathematica kernel seg faults. s = "This is the first line. MAGIC_STRING Everything after this line should get removed. 12345678901234567890123456789012345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890123456789012345678901234567890

Mathematica Table function

时间秒杀一切 提交于 2019-11-29 17:55:26
问题 I'm running a Table function which will take too much time to complete. I wanted to know if there's a way to retrieve the results computed so far. 回答1: The proposed solution Here is a version of Table that is Abort -able and will keep the intermediate results collected so far. It is a modified version of the solution posted here. ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter__List] := Module[{indices, indexedRes, sowTag}, SetDelayed @@ Prepend

Optional named arguments without wrapping them all in “OptionValue”

萝らか妹 提交于 2019-11-29 15:19:33
问题 Suppose I have a function with optional named arguments but I insist on referring to the arguments by their unadorned names. Consider this function that adds its two named arguments, a and b: Options[f] = {a->0, b->0}; (* The default values. *) f[OptionsPattern[]] := OptionValue[a] + OptionValue[b] How can I write a version of that function where that last line is replaced with simply a+b ? (Imagine that that a+b is a whole slew of code.) The answers to the following question show how to

Circular/Angular slider

坚强是说给别人听的谎言 提交于 2019-11-29 15:04:01
问题 A recent SO question reminded me of some code I tried to write a while back. The aim is to make a CircularSlider[] object that can be used for angle-like variables in dynamic objects. The framework for my solution (below) comes from the ValueThumbSlider[] defined in the Advanced Manipulate Functionality tutorial. The main difference is that in ValueThumbSlider[] the value of the slider and the position of the LocatorPlane[] are the same thing, whilst in my CircularSlider[] they are not - and

Can you perform a delayed set (:= in Mathematica) in Matlab?

耗尽温柔 提交于 2019-11-29 14:15:38
So, I've recently converted from Mathematica to Matlab, and while Matlab has most of Mathematica's useful features, I can't figure out how to perform the equivalent of Mathematica's delayed set operation ':=' which assigns a variable a value in a lazy fashion. For example, in Mathematica: y = 2; x := y; y = 3; x would give the value of x as 3, whereas the only way I can get this same behavior in Matlab is: y = 2; x = @()(y); y = 3; x() which, while technically answering my question, is a pretty ad hoc work around and requires treating x as a function. So is there a more natural way to do this

Telling Plot to style vector-valued black-box functions in Mathematica

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:54:36
问题 Suppose I write a black-box functions, which evaluates an expensive complex valued function numerically, and then returns real and imaginary part. fun[x_?InexactNumberQ] := Module[{f = Sin[x]}, {Re[f], Im[f]}] Then I can use it in Plot as usual, but Plot does not recognize that the function returns a pair, and colors both curves the same color. How does one tell Mathematica that the function specified always returns a vector of a fixed length ? Or how does one style this plot ? EDIT: Given

Known issues with copying code from Mathematica to other platforms?

可紊 提交于 2019-11-29 12:57:34
问题 I just noticed that if you have this in MMA (8.0.1 / win7-64): and you copy it to SO (just ctrl-c ctrl-v), you get this: (maxY - minY)/stepy/(maxX - minX)/stepx which is not mathematically equivalent . It should be this: ((maxY - minY)/stepy)/((maxX - minX)/stepx) or this (the InputForm of the above): ((maxY - minY)*stepx)/((maxX - minX)*stepy) It's not caused by StackOverflow's internals as the same happens with a copy to NotePad. Are there more issues like this (especially when working with

How to perform a complicated change of variables for a polynomial (in Mathematica)

亡梦爱人 提交于 2019-11-29 12:42:04
问题 I have an integer polynomial in four variables (w, x, y, and z) that I know can be written as an integer polynomial in these six variables: a = w z b = x y c = w^3 + z^3 d = x + y e = w^3 x + y z^3 f = w^3 y + x z^3 How can I use Mathematica (or maybe Java) to easily do this change of variables? 回答1: Such rewriting can be done by forming a Groebner basis of the replacement polynomials, with respect to a variable order that favors using a-f over w-z. Then use PolynomialReduce with respect to