# Tag Info

45

I think Leonid's answer deserves to be expanded upon. Most other languages are not symbolic, and thus the "variable name" is not something one needs to keep track of --- ultimately the interpreted or compiled code is keeping track of pointers or something. In contrast, in Mathematica the Head of an expression is arbitrary. This is somewhat along the lines ...

34

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is pretty much unacceptable, or at the very least should be the last thing you try. I don't know of a single case where this couldn't be replaced with a better solution Using subscripts is also pretty bad and should be avoided, except ...

21

My answer is based on a modification of a binary heap. Basically the construction looks something like this. We start with a binary tree: Notice that if we label the nodes breadth-first, the labels have an interesting property. Each parent node $n$ has two children, $2n$ and $2n+1$. This also works in reverse: the parent of node $n$ is node $\left\... 17 Preface Below, you will find two different solutions. For understanding the problem itself, the first, iterative solution is better suited since it gives insight in how the solution can be found without directly executing the instructions given as input. Iterative Solution Detailed explanation To explain the idea behind this approach let us work with a ... 16 Many index-specific operations can be implemented via MapIndexed with a level specificaton. Your Power example can be written as: MapIndexed[#1^(#2[[1]]*#2[[2]]) &, test2D, {2}] If you want better readability of indices you can define an auxiliary function: myPower[x_, {n1_, n2_}] := x^(n1 n2); MapIndexed[myPower, test2D, {2}] Some index-specific ... 14 I think the best way to understand this behavior is with this example, In[99]:= x = {a, b, c, d, e}; In[101]:= Length@x Out[101]= 5 In[102]:= x[[3]] = Nothing; In[103]:= Length[x] Out[103]= 4 In[104]:= Block[{Nothing}, Length[x]] Out[104]= 5 When you say x[[3]] = Nothing, you are not deleting an element from x. x is still a 5-element list. But x is ... 13 I have a tree-based method that has the right asymptotics but a very high coefficient. The upshot being, it will not compete with other methods until we get past 10^6 or so in list size. With considerable work that tree structure could be flattened so that Compile might be brought into play. The basic tree layout is {left subtree, node, right subtree} where ... 12 Some years ago, a friend of mine was in the supermarket with his son, small kid, who asked him to buy some candy. After some resistance, my friend agreed, but told him that should be just one. In the cashier, his son had two candy, and my friend said: What is this? haven't we agreed One? And the answer (very smart) was: Yes! Here it is. Zero and One... (... 12 I don't know if this is useful to you but it seems a little cleaner than your own code: asc = <|"z" -> 11, "x" -> 22, "b" -> 33, "a" -> 44|>; keySpan[k_Span][asc_Association] := asc[[k /. First /@ PositionIndex@Keys@asc]] asc // keySpan["x" ;; "a"] asc // keySpan["z" ;; "a" ;; 2] asc // keySpan["b" ;;] <|"x" -> 22, "b" -> 33,... 11 MapIndexed: MapIndexed[#2[[1]] + # &, {a, b, c, d}] {1 + a, 2 + b, 3 + c, 4 + d} Also Range[Length @ #] + # & @ {a,b,c,d} {1 + a, 2 + b, 3 + c, 4 + d} 11 The variable i is a dummy one. The evaluated expression: Sum[f[i], {i, 1, 10}] f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10] contains no explicit variable f[i], hence, the result is 0. Try to first Inactivate the sum, and only then to calculate the derivative: expr1 = D[Inactivate[Sum[f[i], {i, 1, 100}], Sum], f[i]] The result is ... 10 The example JSON string: json = ExportString[<| "Names" -> <| "Sister" -> "Nina", "Brothers" -> {<|"Older" -> "John", "Younger" -> "Jake"|>}, "somethingElse" -> "answer" |>, "DOB" -> { <|"Nina" -> 2001, "location" -> "Miami"|>, <|"John" -> 2017, "location" ->... 10 If you put your cursor on the Position command and press F1 for help, you will see the following under Properties and Relations: "Use Extract to extract parts based on results from Position." There is also an example. For your case: p = Position[{a, b, c, d, e, f}, c] Extract[list,p] where list is the list you want to extract from. 10 My goal is to add an index to all elements of a list in the form {"a", "b", "c", ... }, so it becomes {"N1 a", "N2 b", "N3 c" ... } may be seq={"a","b","c","d"}; MapIndexed["N"<>ToString[First@#2]<>" "<>#1&,seq] gives {"N1 a", "N2 b", "N3 c", "N4 d"} 10 We don't need to avoid Table in my view. In cases that Table is more straightforward, just use Table. If speed is concerned, Compile it. Here is an example: Can I generate a "piecewise" list from a list in a fast and elegant way? Nevertheless, your 2 examples (especially 2nd one) don't belong to the cases that Table is more straightforward, at ... 9 strs = {"first block of text with random content", "different block of text", "1 2 3 4"}; Nearest[(StringPadRight[#, 50] & /@ strs), "content random with"] This is a deep and complex question apparently: Mathematica has a menagerie of built in goodies to assemble your own variant. EditDistance DamerauLevenshteinDistance ... 9 As of Mathematica 11: filenames = Table[CreateFile[], 3]; content = {"first block of text with random content", "different block of text", "1 2 3 4"}; MapThread[Put, {content, filenames}]; index = CreateSearchIndex[filenames]; Perform searches using TextSearch: Snippet /@ Normal@TextSearch[index, "block"] In order to rank search results, score them ... 9 Following the comments I am encouraging the use of brackets rather than subscripts or superscripts. Here is an example where a function may take a variable with a subscript or a variable without a subscript. The function will use pattern recognition to sort out how to behave. First we define the function. Note I start with a ClearAll[f] so that previous ... 7 Prompted by comments conversation with Mr. Wizard, a routine I use: findMultiPosXX[list_, find_, allowBits_: False, skipCands_: True] := Module[{f = DeleteDuplicates[find], o, l, oo, bitmax = 20, cands, dims}, If[allowBits && Length@f <= bitmax, With[{r = If[Length@(dims = Dimensions@list) == 1, Range@Length@list, Array[... 7 Using DownValues enables you to format the display in the subscripted form without using Notation and Symbolize (Format[#[n_]] := Subscript[#, n]) & /@ {x, σ, a}; kvar[k_] := Through[{x, σ, a}[k]] kvar[3] kvar[n] If you will never use a symbolic index then you can restrict the argument of kvar to Integer as you did originally. 7 What are the requirements for well behaved variables? Functions are not variables, although in most cases, the kernel treats undefined variables and functions identically. Sometimes it doesn't. After all, there are places in mathematics where the difference between a number and a function is important. One extreme and undocumented example is Dt[], the ... 7 func1[a_, b_] := a + b; func2[a_, b_] := 1 + a + b; func3[a_, b_] := a + 2 b; n = 10; sa = SparseArray[{Band[{1, 1}] -> (func1[0, #] & /@ Range[n]), Band[{1, 2}] -> (func2[0, #] & /@ Range[n - 1]), Band[{2, 1}] -> (func3[0, #] & /@ Range[n - 1])}, {n, n}]; sa // MatrixForm or sa = Quiet@SparseArray[{Band[{1, 1}] -> (... 7 Try Pick[foo, mask] (* {a, c}*) 6 Module[{p = Range[Length@#]}, Reap@Fold[(Sow[#[[#2]]]; Drop[#, {#2}]) &, p, #]] &@{1, 1, 2, 1, 1} (* {{}, {{1, 2, 4, 3, 5}}} *) 6 This seems rather be a error of determining the type of i, j and k than Sum itself. When you introduce your iterator variables in a Module and make clear that they are of type integer, it compiles fine for me: fc = Compile[{{x, _Real, 2}}, Module[{i = 0, j = 0, k = 0}, Sum[x[[i, k]]*x[[j, k]], {k, 5}, {i, 12}, {j, i + 1, 12}]] ] Btw, I want to note ... 6 You could use Evaluate[list] = ConstantArray[0, Length[list]] or MapThread[Set, {list, ConstantArray[0, Length[list]]}] to Set each indexed variable inside of list to 0. If the indexes for the variables inside list follow a known condition, one can use for example p[i_ /; i < 3, j_ /; j < 5] = 0 or memorization p[i_ /; i < 3, j_ /; j < 5] ... 6 An alternative to subscripts as indices... Instead of: {Subscript[x, 1], Subscript[x, 2]} Let's use: x[1], x[2] And this can be generalized to: Subscript[x, i, j] --> x[i,j] This will uniquely identify any number of variables to any dimension. 6 From version 10.4 onward, we can define keySpan like this: keySpan[k1_, k2_] := Replace[<|___, s:PatternSequence[k1 -> _, ___, k2 -> _], ___|> :> <|s|>] so that:$a = <| "z" -> 1, "x" -> 2, "b" -> 3, "a" -> 4 |>; \$a // keySpan["x", "a"] (* <|"x" -> 2, "b" -> 3, "a" -> 4|> *) We can make this more ...

5

You can use this (where t is your dataset): Ordering[#, -1] & /@ Transpose[t] which produces {{1}, {1}, {2}, {4}, {2}, {2}} Incidentally, the list of positions you gave in your question is wrong (the 4th element should be {4,4}, and the 6th element should be {2,6}). The above method omits the first coordinates in your expected output, since they ...

5

This is indeed somewhat confusing when you are new to Mathematica. In Mathematica, == stands for mathematical equality. Thus a == 0 does not evaluate to either True or to False until a is replaced by a numerical value. a is considered to be a variable that may or may not be zero. A pattern like x_ /; condition will only match if condition is explicitly ...

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