Tag Info

Hot answers tagged

55

I. General I will first try to briefly answer the questions, and then illustrate this with a small but practical application. 1.Speed of insertion / deletion Associations are based on so called Hash Array Mapped Trie persistent data structure. One can think of this as a nested hash table, but it is more than that, because it has the following properties: ...


37

As an Eterprise CDF user, I can say I have really tried, and my current opinion is that creating a standalone GUI program with the Wolfram Language is not an easy/commercial/deliverable task at the moment. Here are my points: All the interface controls are very limited. You will have a lot of difficulty to do basic things like make Tab jump between fields, ...


31

Between Versions 7 and 8 Hash now gives the hash of a raw sequence of characters when applied to Strings. In past versions the string characters (quotation marks) were included in the calculation of the hash. (Reference) Use "\"" <> string <> "\"" before hashing if you want output to match older versions. \[Dash], \[LongDash] and ...


30

Compose and Composition There is, but it is deprecated (in favor of Composition): Compose: MapThread[Compose, {{a, b, c}, {1, 2, 3}}] (* {a[1], b[2], c[3]} *) I still use Compose myself, but I would not take the responsibility to recommend this as a common practice. You can also use Composition[#1][#2] &, although this is hardly better than your ...


29

First let me note that I didn't write PositionIndex, so I can't speak to its internals without doing a bit of digging (which at the moment I do not have time to do). I agree performance could be improved in the case where there are many collisions. Let's quantify how bad the situation is, especially since complexity was mentioned! We'll use the ...


23

Updated Both Hold and Inactive block evaluation; the key difference is that Inactive is meant to be wrapped around heads rather than a whole expression. Inactivate does this. Inactivate[1 + 2 + 3 * 4 ^ 5 ] // FullForm Inactive[Plus][1, 2, Inactive[Times][3, Inactive[Power][4, 5]]] It is of course possible to use Inactive directly, and it will behave ...


22

I helped design Association, and I designed and implemented Dataset, so I wanted to comment on question 3: Dataset is designed explicitly for hierarchical data. It supports any 'shape' of data, inferring the shape when the Dataset is first created. It also tracks the shape of the data as transformations are applied to the dataset, using a type-inference ...


19

Let's look at this with a simple example without considering complicated indexing and levels. Consider the list (the colours are merely for visual guidance): A = Array[Subscript[a, ##] &, {2, 3, 4}] Dimensions@A (* {2, 3, 4} *) This is a list containing 2 lists, each of which contains 3 sublists, which in turn contain 4 elements of the array. ...


19

The Wolfram Language is what we all know as Mathematica, but rebranded to help wider adoption to people, particularly for people who don't self-identify as "math" people. As a Mathematica programmer, emphasis on the "programmer", I see this as a good thing.


17

What the first part of the variable declaration does Manipulate initializes complexparts to {Re[#], Im[#]} & when it executes. (In general, a declaration of the form {{var, expr},...} in a Manipulate results in the local variable var being initialized to expr.) To use complexparts outside of the Manipulate, do this: complexparts = {Re[#], Im[#]} ...


17

Good News Everyone! Two-parameter syntax for Fold and FoldList has been (silently) implemented! Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well. Using version 10: Fold[f, a] FoldList[f, a] f[f[f[1, 2], 3], 4] {1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]} And the held expression example: ...


16

# is a placeholder for an expression. If you want to define a function, $y(x)=x^2$, you just could do: f = #^2 & The & "pumps in" the expression into the # sign. That is important for pairing & and # when you have nested functions. f[2] (* 4 *) If you have a function operating on two variables, you could do: f = #1 + #2 & So ...


16

Everything depends on what you try to do with this. First you could use Subsuperscript and get rid of one level which is introduced by your Power Subscript[e,3]^2//FullForm (* Power[Subscript[e,3],2] *) Subsuperscript[e,3,2]//FullForm (* Subsuperscript[e,3,2] *) Both forms look equally in the front-end, but now you can use TagSet to transform all e with ...


15

I'm guessing you're coming from a programming language where every expression must evaluate to a value, and if it didn't evaluate to something (like 5[Cos+Sin]), it's a syntax error. To me, Mathematica started to make a lot more sense, once I stopped thinking about functions and values, and started to think of every expression as evaluating to an "expression ...


15

This is interesting: If you replace the symbol y with the string "y", Append works fine. Append[ds, <|"a" -> 5, "b" -> "y"|>] Also, if you start out with Symbols, then it works fine: ds = Dataset[{<|"a" -> 1, "b" -> x|>, <|"a" -> 2, "b" -> y|>, <| "a" -> 3, "b" -> z|>, <|"a" -> 4, "b" -> ...


13

Let's start by taking a look at the compiled form of one of our queries: Dataset`CompileQuery[Query @ First @ spans] (* Dataset`WithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: Dataset`WithOverrides, GeneralUtilities`Checked and ...


13

One difference is that NDSolve directly supports Inactive. It can be used to specify operators such as divergence ($\nabla\cdot$) without automatically evaluating them to components. This is described here.


12

My experience is that while Mathematica does present some headaches with creating consistent layouts the limitations in creating a professional looking app are limited by your ability to do graphic design. For example most reading this could create a web page. But how many could create a cool looking web page? So there are two aspect: underlying code and ...


11

I'm afraid my comment was too obscure to be noticed. Further, I disagree with one premise somewhere in the commentary, and I wish to make a fuller explanation to see if I understand correctly or incorrectly. Finally, I think the question is answered in the documentation on the Standard Evaluation Procedure: Evaluate the head of the expression. ...


11

The problem is just name collisions, that isn't at all abstract and will happen in any programing language, so it would be odd to claim that it's impossible due to the way Mathematica works. The solution is simply to name your parameters when you write your functions so they don't collide, so you write for instance: RegionFunction -> Function[{a1, b1}, ...


10

The less trivial problem is to calculate the product as fast as possible. Some performance tests (from slowest to fastest): SeedRandom[0]; a = RandomReal[{0.999, 1.001}, 10000000]; Det@DiagonalMatrix[a] // AbsoluteTiming (* I don't have 1 PB RAM :) *) Product[a[[i]], {i, Length[a]}] // AbsoluteTiming (* {5.516518, 0.323758} *) 1 ## & @@ a // ...


10

Note that sin is not a function in Mathematica, instead use Sin with a capital S and functions use square brackets to hold their arguments. So Sin[x] instead of sin(x) or even sinx as you have it. You can use NestWhile or NestWhileList (for the list of values) also for this problem: f[x_] := x - 0.8 - 0.2 Sin[x] newtonsMethod[foo_, k_, s_: 0.0001] := ...


9

Here is another way: you can fool the depth-1 tag rule for UpValues with a few temporary symbols. Here is an example: ClearAll[e]; e /: Subscript[e, i_?IntegerQ] := e /: Subscript[e, i] = Module[{el}, el /: el^p_ := el /: el^p = Module[{elp}, elp /: NumericQ[elp] = True; Format[elp] := TraditionalForm[Subscript["e", i]^p]; ...


9

WordData can give you the IPA form of a word: Gather[ WordData[#, "PhoneticForm"] & /@ {"pray", "prey", "wade", "weighed"} ] (* {{"pr'ey", "pr'ey"}, {"w'eyd", "w'eyd"}} *) EDIT It seems WordData[word, "PhoneticForm"] no longer provides the proper IPA, however that data is still included in the paclet so we can make a new WordData property for that. ...


9

From the docs for Label Label must appear as an explicit element of a CompoundExpression object. So this works: p[1] = .9; i = 1; Label[begin]; i++; p[i] = p[i - 1] + 1; Print[i]; If[i < 5, Goto[begin], Goto[end]]; Label[end]; But this doesn't: p[1] = .9; i = 1; Label[begin]; i++; p[i] = p[i - 1] + 1; Print[i]; If[i < 5, Goto[begin], ...


9

expr = Cos[3 x] + \[Pi]/cofe Exp[y^(c z)]; Cases[expr, _Symbol?(! NumericQ[#] &), Infinity] // Union (* {c, cofe, x, y, z} *) In this case the Union is not necessary; however, in general it is needed to eliminate duplicates.


9

You need to use InputForm to see the actual value of the numbers. x = -15*0.1 + 6*0.1; InputForm[x] Now when you wrote a[-15*0.1 + 6*0.1] = 5 Now 5 is actually stored in a[-0.8999999999999999] and not in a[-0.9], this is why a[-0.9] does not return the value 5 you expected. But when you wrote a[-0.8999999999999999] it did. Now, as to why ...


9

Save defaults before any changes attrLog = Log // Attributes; ClearAttributes[Log, Listable] Log // Attributes {NumericFunction, Protected} Restore defaults Attributes[Log] = attrLog {Listable, NumericFunction, Protected}


9

If you have not saved the attributes before changing them, and also can't quit the Kernel, then you could launch a Subkernel and get the original attributes that way: ClearAttributes[Log, Listable] Attributes[Log] {NumericFunction, Protected} First@ParallelEvaluate[Attributes[Log]] {Listable,NumericFunction,Protected}


8

While the tutorial will undoubtedly explain better that I could the entire topic of pure functions, which is what Slot, or # has to do with, I'll answer the specific question at hand. The Slot is treated as the argument in an anonymous function. Specifically, the code #^2 & // FullForm Reveals that what is actually going on is ...



Only top voted, non community-wiki answers of a minimum length are eligible