New answers tagged

11

You can use EntityValue to find out what symbols can be atomic: EntityValue[EntityClass["WolframLanguageSymbol", "Atomic"], "CanonicalName"] {"AggregationLayer", "Association", "Audio", "BasicRecurrentLayer", "BatchNormalizationLayer", "BooleanFunction", "BoundaryMeshRegion", "ByteArray", "CatenateLayer", "ColorProfileData", "Complex", "...


3

The answer is that any head can be the head of a non-atomic expression. Thus, you cannot tell an atomic expression by its head, you must test it with AtomQ.


2

Indeed, it is impossible if you do not evaluate the full expression first. Here is an example: bla /: Map[bla, _] := SparseArray[{56, 1}] AtomQ[Map[bla, {56, 0}]] AtomQ[Map[# &, {56, 0}]] True False The problem here: SparseArrays are atomic, but lists are not.


4

I ended up finding my answer in a roundabout way while pursuing another issue. According to https://reference.wolfram.com/language/tutorial/BasicObjects.html: All expressions in the Wolfram Language are ultimately made up from a small number of basic or atomic types of objects. These objects have heads that are symbols that can be thought of as "...


1

Replace[exp1, a:(_Real|_Complex) :> SetPrecision[a, 10], All]


5

I'm assuming the OP wants "\\sqrt{2}" to turn into the equivalent of $\sqrt{2}$ or $\sqrt{2}$, which would Sqrt[2] in Mathematica. The command ToExpression[s, TeXForm] does not seem to handle the square roots, when they are embedded in a matrix, as in the OP's string. The matrix format of TeX is so straightforward that one can split it up into a list of ...


4

From your InputForm example, we can see that each row has the same structure. Namely each row has the form TextCell[Row[List[" ", TextCell[element_, "InlineFormula"], " "]]] or TextCell[Row[List[" ", ExpressionCell[element_, "InlineFormula"], " "]]] where element is the number in that entry of the matrix. Then we can take advantage of this and simply ...


3

Straight from the documentation: Coefficient[expr,form,0] picks out terms that are not proportional to form. Coefficient[a + b + c + x + y + z + r*E^m + s/E^m + t*E^n + u/E^n,E,{0,m,-m,n,-n}] // Print (* {a + b + c + x + y + z, r, s, t, u} *) Try it online!


4

assoc /. Association -> (Values@*List) // Flatten {0, "one", 2, 3, 4} When some values are non-atomic this approach and the deepest Level approach give different results: assoc2 = <| "a" -> foo[0], "b" -> <| "c" -> "one" , "d" -> 2 , "e" -> 3|>, "f" -> {bar[1], foo[3]}|>; assoc2 /. Association -> (Values@*List) ...


7

Level[assoc, {-1}] (* {0, "one", 2, 3, 4} *)


4

Map[ AssociationMap[ keyTest[#[[1]]] -> valueTest[#[[2]]] & ], <|"a" -> 0, "b" -> <|"c" -> "one", "d" -> 2, "e" -> 3|>, "f" -> 4|>, {-2} ] <|"a" -> 0, "b" -> <|keyTest["c"] -> valueTest["one"], keyTest["d"] -> valueTest[2], keyTest["e"] -> valueTest[3]|>, "f" -> 4|>


3

You can use the Listable attribute. Give the attribute to h or a wrapper-function as below: Module[{hh}, SetAttributes[hh, Listable]; hh[x__] := h[x]; hh@{{u[a], u[b]}, {u[v[c]], d}, {{d, e}, {f, g}}} ] (* {{h[u[a]], h[u[b]]}, {h[u[v[c]]], h[d]}, {{h[d], h[e]}, {h[f], h[g]}}} *) Also: Function[, h[##], Listable]@{{u[a], u[b]}, {u[v[c]], d}, {{d, e},...


5

You can use Replace with a level of All: Replace[ {{a,b},{c,d},{{d,e},{f,g}}}, {x:Except[_h],y:Except[_h]} :> h[x], All ] {h[a], h[c], {h[d], h[f]}} Using Replace with a level of All does a depth first (bottom up) replacement. Using ReplaceAll instead uses a top down replacement: ReplaceAll[ {{a,b},{c,d},{{d,e},{f,g}}}, {x:Except[...


5

ReplaceAll with replacement rule {x_, y : Except[___List]} :> h[x]: {{a, b}, {c, d}, {{d, e}, {f, g}}} /. {x_, y : Except[___List]} :> h[x] {h[a], h[c], {h[d], h[f]}} {{u[a], u[b]}, {u[v[c]], d}, {{d, e}, {f, g}}} /. {x_, y : Except[___List]} :> h[x] {h[u[a]], h[u[v[c]]], {h[d], h[f]}}


7

You can use ReplaceAll with the replacement rule {{x__}} :> {x}: stripDoubleBraces = # /. {{x__}} :> {x} & stripDoubleBraces @ {{1, 2, 3}} {1, 2, 3} stripDoubleBraces @ {{1, 2, 3}, {4, 5}} {{1, 2, 3},{4,5}} If you need to the stripping work with arbitrarily deep nesting, you can replace ReplaceAll with ReplaceRepeated: {{1}, {{{{{{{5}}}}...


0

Slightly simpler, but basically equivalent to the accepted answer is: SetAttributes[a, Flat]; GatherBy[expressions, ReplaceAll[A->a]] {{A[B[x, x], A[x, A[x, x]]], A[A[A[B[x, x], x], x], x]}}


1

Flatten is a nice approach. You can also do: ClearAll[asIfAWereAssociative] asIfAWereAssociative = Block[{a}, SetAttributes[a, Flat]; # /. A -> a] &; expressions = {A[B[x, x], A[x, A[x, x]]], A[A[A[B[x, x], x], x], x]}; Equal @@ (asIfAWereAssociative /@ expressions) True GatherBy[expressions, asIfAWereAssociative] {{A[B[x, x], A[x, A[x, x]]],...


0

Ok, the answer seems to be the following, expressions need to be compared using flatten, which detects associative equivalence for A based on supplying A as third argument of flatten and specifying Infinity to check at all levels (of bracket nesting) Flatten[A[B[x, x], A[x, A[x, x]]], Infinity, A] == Flatten[A[A[A[B[x, x], x], x], x], Infinity, A] ETA: ...


3

Yes, you can always look at the FullForm of an expression. I'm using Hold in my example so that Mathematica doesn't try to evaluate any of the things: Hold[ { f[a,b,c], a[[1]], a:=1 } ]//FullForm (* Hold[List[f[a,b,c],Part[a,1],SetDelayed[a,1]]] *)


2

Here's a variation of kglr's answer that doesn't modify A or B. First define a helper function h: h[A[___?AtomQ] | A[_]] := Sequence[] h[B[_] | B[]] := Sequence[] h[a_] := a Then use the helper function in your Groupings call: Groupings[IntegerPartitions[3], {h @* A -> {2, Orderless}, h @* B -> {2, Orderless}}] {B[2, 1], A[B[1, 1], 1], B[B[1, 1], ...


2

ClearAll[a, b] a = A; b = B; a[___?AtomQ] := Sequence[] b[_] := Sequence[] Groupings[IntegerPartitions[3], {a -> {2, Orderless}, b -> {2, Orderless}}] {B[2, 1], A[B[1, 1], 1], B[B[1, 1], 1]}


2

Ok, I found the answer: Groupings[IntegerPartitions[3], {A -> {2, Orderless}, B -> {2, Orderless}}] this returns: {A[2, 1], B[2, 1], A[A[1, 1], 1], A[B[1, 1], 1], B[A[1, 1], 1], B[B[1, 1], 1]}


Top 50 recent answers are included