Tag Info

New answers tagged

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 = ...


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] ...


0

Malte Lenz is correct; there are no level one expressions in l therefore the Map operation appears inert. (The default levelspec of Map is {1}.) Observe that if a levelspec of {0} is used the func is applied: Map[func, l, {0}] func[l] If you are asking why conceptually Map works this way I can only say that in my experience the existing behavior has ...


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!


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]]


0

lst = {"0/0", "1/25", "1/36", "1/49"}; Temporarily change the definition of Indeterminate to Missing["NotAvailable"]: Block[{Indeterminate = Missing["NotAvailable"]}, Quiet@ToExpression@lst] {Missing["NotAvailable"], 1/25, 1/36, 1/49}


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}


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"


2

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


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], {"{" | "}" -> ...


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] ...


0

I played around with the levelspec of Map and apparently this combination works. I have no idea what magic makes this work, though: Map[Association, lst, {-6, -3}] Or the following (from @Kuba's simultaneous edit): Map[Association, lst, {0, -3}]


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 ...


0

I tried this with a couple of different level specifications in Map, but it seems once the outer List becomes an Association, the other levels of Map are not used. So, instead, we apply it twice: Map[Association]@Association@lst (* <|a -> <|1 -> {A1, A2, A3, A4}, b -> 2|>, c -> <|3 -> C3, 4 -> C4|>|> *)


9

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


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

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 -> ...


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"}}


0

"Xabcde" /. a_ :> StringCases[a, _] /. {"X", b__} :> StringJoin[b] (*"abcde"*)


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 ...


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}


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}


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 -> ...


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

Try also this: as = <|"Aa" -> "1/2", "Av" -> "2/5", "Ca" -> "3/4", "Bx" -> "3/7", "Ce" -> "4/9"|>; ks = KeySelect[as, Characters[#][[1]] == "A" || Characters[#][[2]] == "a" &]; op = Map[ToExpression, ks, 2]; Merge[{Association[Complement[Normal[as], Normal[ks]]], op}, Total] yielding (* <|"Bx" -> "3/7", ...


1

cr = CoefficientRules[myExpr, {Delta, phi}] cr /. HoldPattern@(a_ -> b_) :> f @@ a -> b FromCoefficientRules[cr /. HoldPattern@(a_ -> b_) :> a -> f @@ a, {Delta, phi}] (* f[0, 0] + Delta f[1, 0] + Delta phi f[1, 1] *)


4

A variation on the theme: test = {Aa -> "1/2", Av -> "2/5", Bx -> "3/7", Ce -> "4/9"}; test /. (key_ /; StringMatchQ[ToString[key], ___ ~~ "a" | "A" ~~ ___] -> val_) :> (key -> ToExpression[val]) (*{Aa -> 1/2, Av -> 2/5, Bx -> "3/7", Ce -> "4/9"}*)


6

I would use something like this: Clear[f]; f[key_ -> val_] := key -> If[ StringMatchQ[ToString@key, ___ ~~ "A" | "a" ~~ ___], q@val, val] then f /@ {Aa->1/2, Av->2/5, Bx->3/7, Ce->4/9} (* {Aa -> q[1/2], Av -> q[2/5], Bx -> 3/7, Ce -> 4/9} *) If you have v10, and your data is in an Association, you use AssociationMap, ...



Top 50 recent answers are included