Tag Info

Hot answers tagged

9

The undocumented function ToAssociations in the GeneralUtilities package does this In[3]:= Needs["GeneralUtilities`"] In[4]:= ToAssociations[lst] (* Out[4]= <|a -> <|1 -> {A1, A2, A3, A4}, b -> 2|>, c -> <|3 -> C3, 4 -> C4|>|> *) As with any undocumented function, use with caution, as contents tend to shift during ...


9

Not pretty but works: lst //. x : {__Rule} :> Association[x]


9

list = {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c -> "cccc", d -> "dddd"e -> Missing["NotAvailable"], f -> Missing["NotAvailable"]}} DeleteCases[list, Rule[_, _Missing], Infinity] {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c -> "cccc", d -> "dddd"}}


4

Use ComplexExpand expr = Abs[x + I y] + Sin[Abs[c + I (d + e)]]; expr // ComplexExpand Sqrt[x^2 + y^2] + Sin[Sqrt[c^2 + (d + e)^2]]


4

I would love this to be uniform for both integer and rational numbers a2 = {2/3, 4/5, 9/7, 3/7, 1.5, 3, 1/9}; StringTrim@StringJoin[" " <> ToString[#, InputForm] & /@ a2] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) Row[ToString[#, InputForm] & /@ a2, " "] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) StringReplace[ToString[a, InputForm], {"{" | "}" -> ...


4

Use StringReplace["Xabcde", "X" ~~ e__ -> e]. Replace, et al are for lists/expressions... Notice that AtomQ@"Xabcde" is True, so regular (non-string) replace operations only "see" it as a singular entity: "Xabcde" /. "Xabcde" -> 1 (* 1 *) From the docs for ReplaceAll: "... to transform each subpart..." - but there is no "subpart" for atoms, so ...


4

you can use DeleteMissing also l = {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c -> "cccc", d -> "dddd", e -> Missing["NotAvailable"], f -> Missing["NotAvailable"]}}; Normal@DeleteMissing[Association@l, Infinity] (*{First -> {a -> "aaaa", b -> "bbbb"},Second -> {c -> "cccc", d -> "dddd"}}*)


4

Here is another alternative using UpValues: Block[{Missing}, Missing /: _ -> Missing["NotAvailable"] := Sequence[]; {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c -> "cccc", d -> "dddd", e -> Missing["NotAvailable"], f -> Missing["NotAvailable"]}} ] (* {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c ...


3

Everything is as it should be. In the first instance, you want a separate rule for each element of the list. For that, you can use Thread: Subscript[Ε, si] = {Ex, Ey, Ez}; Subscript[B, si] = {Bx, By, Bz}; Subscript[S, si] = 1/Subscript[μ, 0] (Subscript[Ε, si]* Subscript[B, si]); Subscript[S, si] /. Thread[Subscript[B, si] -> Subscript[B, si]/c] ...


3

Less elegant than DeleteCases, however an alternative: exp = {First -> {a -> "aaaa", b -> "bbbb"}, Second -> {c -> "cccc", d -> "dddd", e -> Missing["NotAvailable"], f -> Missing["NotAvailable"]}} Delete[exp, Position[exp, _ -> _Missing]] Or Delete[exp, Drop[#, -1] & /@ Position[exp, _Missing]] (*{First -> ...


2

You can also use a combination of StringSplit, SyntaxQ and Pick: str = "{Aaaa -> a, Bbbb- > b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; str2 = Pick[#, SyntaxQ /@ #] &@StringSplit[str, "," | "{" | "}"] (* {"Aaaa -> a", " Ddddd -> c", " Fffff -> e"} *) ToExpression@str2 (* {Aaaa -> a, Ddddd -> c, Fffff -> e} *) ...


2

You can use StringCases. str = "{Aaaa -> a, Bbbb -> b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; ToExpression@StringCases[str, WordCharacter .. ~~ " -> " ~~ WordCharacter ..] {Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}


2

Or StringTake[ToString[a, FormatType -> InputForm], {2, -2}] The inelegant use of StringTake strips off the leading and trailing brackets.


2

Your equation is easier to handle if it put into an equivalent form. eq1 = Sqrt[l1] + Sqrt[t1] + Sqrt[w1] == d; It is also to convenient to write the rules as r1 = {l1 -> (pw^2 w1)/((1 + a e1)^2 w^2)}; r2 = {t1 -> (pw^2 w1)/((1 + a e1)^2 r^2)}; and substitute u^2 for w1 getting a new equation eq2 = eq1 /. Join[r1, r2] /. w1 -> u^2 ...


2

Possibly you would be pleased with an alternative approach: lst = {"0/0", "1/25", "1/36", "1/49"}; Quiet @ ToExpression @ lst /. Indeterminate -> Missing["Unavailable"] {Missing["Unavailable"], 1/25, 1/36, 1/49}


2

How about something like this? myfun[x_] := If[x == "0/0", Missing["Unavailable"], ToExpression[x]] Map[myfun, lst] {Missing["Unavailable"], 1/25, 1/36, 1/49}


2

expr = Abs[x + I y] + Sin[Abs[c + I (d + e)]]; expr /. Abs[a_ + I*b_] -> Sqrt[a^2 + b^2] (* Sqrt[x^2 + y^2] + Sin[Sqrt[c^2 + (d + e)^2]] *) Have fun!


1

First, a fixed version of your code: ClearAll[eOF0, eOF1, eOF2, eOF3] eOF0[int_, dt_, nas_, nts_] := Module[{d = ConstantArray[2, {nas + 1, nts}]}, For[j = nts, j > 1, j--, d = ReplacePart[d, {1, j - 1} -> (1 - int*dt)*d[[1, j]]]]; Grid[d]] eOF0[0.01, 0.1, 4, 4] and a few alternatives eOF1[int_, dt_, nas_, nts_] := Module[{d = ...


1

Because in your first case, Map[func,l] will evaluate to verbatim l while building up the replacement rules. When the replacement is then done, the replacement rule used is x[l_] -> l. My guess why Map[func, l] evaluates the way it does is that Map works by "inserting" func into it's second argument at the default mapping level, 1. As there is no such ...


1

Terse: a = {1, 17, 2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; ToString @ Row[InputForm /@ a, " "] "1 17 2/3 4/5 9/7 3/7 1/7 1/9"


1

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; StringJoin@Cases[a, Rational[x_, y_] :> " "<>ToString[x] <> "/" <> ToString[y]] reply to comment: a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9, 5, 6, 99/10}; f[Rational[x_, y_]] := " " <> ToString[x] <> "/" <> ToString[y]; f[x_] := " " <> ToString[x]; StringJoin[f[#] & /@ a] ...


1

Or the StringReplace version: In[3]:= StringReplace[str, WordCharacter .. ~~ " -> ," -> ""] Out[3]= "{Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}" In[4]:= ToExpression@% Out[4]= {Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}


1

ToExpression[ "{a-> POR, b-> D610, c-> 0, d-> \"300/7\", e -> \"1/400\"}" ] /. s_String :> Defer @ ToExpression[s] {a -> POR, b -> D610, c -> 0, d -> ToExpression["300/7"], e -> ToExpression["1/400"]} response to comment: ToExpression[ "{a-> POR, b-> D610, c-> 0, d-> \"300/7\", e -> ...



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