wolfram-mathematica

PatternTest not optimized?

妖精的绣舞 提交于 2019-12-05 03:51:55
In preparing a response to An unexpected behavior of PatternTest in Mathematica I came across an unexpected Mathematica behavior of my own. Please consider: test = (Print[##]; False) &; MatchQ[{1, 2, 3, 4, 5}, {x__?test, y__}] During evaluation of In[1]:= 1 During evaluation of In[1]:= 1 During evaluation of In[1]:= 1 During evaluation of In[1]:= 1 False Since, as Simon's quote of the documentation concisely states: In a form such as __?test every element in the sequence matched by __ must yield True when test is applied. I am wondering why Mathematica is testing the first element of the list

Mathematica — why does TreeForm[Unevaluated[4^5]] evaluate the 4^5?

独自空忆成欢 提交于 2019-12-05 03:49:44
If I give Mathematica the input TreeForm[Unevaluated[4^5]] I expect to see three boxes -- power, 4, and 5. Instead I see a single box with 1024. Can anyone explain? Compare TreeForm@Unevaluated[4^5] with TreeForm@Hold[4^5] From the help: Unevaluated[expr] represents the unevaluated form of expr when it appears as the argument to a function. and Hold[expr] maintains expr in an unevaluated form. so, as Unevaluated[4^5] gets to TreeForm ... it gets evaluated ... It works like this: f[x_+y_]:=x^y; f[3+4] (* -> f[7] *) f[Unevaluated[3+4]] (* ->81 *) A level of Unevaluated is stripped off with every

Concatenate two integers in Mathematica 7

坚强是说给别人听的谎言 提交于 2019-12-05 03:30:58
What is the most efficient way to concatenate two positive integers in Mathematica 7? cc[123, 4567] >> 1234567 What about more than two? cc[123, 4, 567, 89] >> 123456789 This will be slightly faster for many integers, than your last solution: ToExpression[StringJoin @@ Map[IntegerString, {##}]] & A more concise alternative is to accept a single argument, assuming it to be a list, rather than a sequence, of numbers to concatenate: ToExpression@StringJoin@IntegerString@#& which is based on IntegerString being Listable . This only works properly for short integers because the output must be

Is it possible to delete “context`” from the list of loaded Contexts[]?

爷,独闯天下 提交于 2019-12-05 03:25:12
We can remove all symbols in a particular context by using Remove["context`*"] . But is it possible to remove "context`" itself from the system so that it will no longer be listed in Contexts[] ? As far as I can tell (a guess), a context is automatically removed from Contexts[] once it becomes empty (has no symbols). At least, this happens in my tests. Here is one: In[1]:= BeginPackage["Test`"] EndPackage[] Out[1]= Test` In[3]:= MemberQ[Contexts[],"Test`"] Out[3]= False In[4]:= Test`a Out[4]= a In[5]:= MemberQ[Contexts[],"Test`"] Out[5]= True In[6]:= Remove["Test`*"] In[7]:= MemberQ[Contexts[]

Using All in MapAt in Mathematica

巧了我就是萌 提交于 2019-12-05 02:49:54
I often have a list of pairs, as data = {{0,0.0},{1,12.4},{2,14.6},{3,25.1}} and I want to do something, for instance Rescale , to all of the second elements without touching the first elements. The neatest way I know is: Transpose[MapAt[Rescale, Transpose[data], 2]] There must be a way to do this without so much Transpose ing. My wish is for something like this to work: MapAt[Rescale, data, {All, 2}] But my understanding is that MapAt takes Position -style specifications instead of Part -style specifications. What's the proper solution? To clarify, I'm seeking a solution where I don't have to

'StringCut' to the left or right of a defined position using Mathematica

两盒软妹~` 提交于 2019-12-05 02:42:08
On reading this question , I thought the following problem would be simple using StringSplit Given the following string, I want to 'cut' it to the left of every "D" such that: I get a List of fragments (with sequence unchanged) StringJoin @fragments gives back the original string (but is does not matter if I have to reorder the fragments to obtain this). That is, sequence within each fragment is important, and I do not want to lose any characters. (The example I am interested in is a protein sequence (string) where each character represents an amino acid in one-letter code. I want to obtain

histogram without vertical lines in Mathematica

旧城冷巷雨未停 提交于 2019-12-05 02:14:25
I am trying to make an histogram without vertical lines. I'd like to have a plot which looks like a function. Like this: The same question has been asked for R before ( histogram without vertical lines ) but I'm on Mathematica. I have been looking into the ChartStyle options without success. There probably are ways to do this by fiddling with EdgeForm[] and FaceForm[] in Histogram , but I've found it simpler to roll one on my own, whenever I need it. Here's a very simple and quick example: histPlot[data_, bins_, color_: Blue] := Module[{ countBorder = Partition[Riffle[Riffle[#1, #1[[2 ;;]]],

Splitting a list based on another list values in Mathematica

风流意气都作罢 提交于 2019-12-05 02:07:52
问题 In Mathematica I have a list of point coordinates size = 50; points = Table[{RandomInteger[{0, size}], RandomInteger[{0, size}]}, {i, 1, n}]; and a list of cluster indices these points belong to clusterIndices = {1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1}; what is the easiest way to split the points into two separate lists based on the clusterIndices values? EDIT: The solution I came up with: pointIndices = Map[#[[2]] &, GatherBy[MapIndexed[{#1, #2[[1]]} &, clusterIndices],

How can I make Mathematica kernel pause for an external file creation

£可爱£侵袭症+ 提交于 2019-12-05 01:34:13
Is it possible to pause Mathematica kernel during a computation? Here is an example. Module[{}, Mathematica code.... .......... .......... { Calls an external program with some argument Needs to wait for an external program to create a file (* How ?*) } Mathematica code using that file content.... ........... ........... ] I can come up with a Do[..] loop solution that keeps on checking in a specified directory whether a file is created or not. Once it finds the file it reads the content and rest of the Mathematica code processes the data. Is there any elegant way to solve this problem? BR Try

Finding previously defined messages in Mathematica

半世苍凉 提交于 2019-12-05 01:32:58
问题 Mathematica by default defines a lot of useful messages for signaling common errors, like functions being called with the wrong number of arguments or files not being found. In general, I prefer to use existing, defined messages wherever possible, because it makes it easier for them to be handled via mechanisms like Check , Quiet and On / Off . However, all my attempts at finding what messages are currently defined have failed; obvious approaches like DownValues[MessageName] don't work at all