DMUG-Archiv 2020

Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

Re: [Dmug] Warum [Name]

Hallo Peter,

worin unterscheidet sich denn

In[10]:= #[[0]] & /@ (#["Name"] & /@ IsotopeData["U238", "BranchingRatios"])

Out[10]= {1.00, 5.45*10^-7, 2.2*10^-12}


von


In[11]:= IsotopeData["U238", "BranchingRatios"]

Out[11]= {1.00, 5.45*10^-7, 2.2*10^-12}

was soll das? Worauf bezieht sich Ihre Frage?


Wenn man Probleme mit der funktionnalen Programmierung hat, bearbeitet man am besten Leerlaufbeispiele, also solche, die nicht evaluieren, denn daran sieht man, was wohin abgebildet wird:

Das

In[12]:= #Y & /@ {X[1], X[2], X[3], X[4]}

During evaluation of In[12]:= Function::slot1: (#Y&)[X[1]] is expected to have an Association as the first argument.

During evaluation of In[12]:= Function::slot1: (#Y&)[X[2]] is expected to have an Association as the first argument.

During evaluation of In[12]:= Function::slot1: (#Y&)[X[3]] is expected to have an Association as the first argument.

During evaluation of In[12]:= General::stop: Further output of Function::slot1 will be suppressed during this calculation.

Out[12]= {#Y, #Y, #Y, #Y}


ist klar ein Syntaxfehler. Wie funktioniert der Slot (https://reference.wolfram.com/language/ref/Slot.html)  in Mathematica?

Er (der Slot) vertritt das erste Argument, wenn es nur ein Argument hat, kann man das Argument in Map[] weglassen:


In[13]:=  Y /@ {X[1], X[2], X[3], X[4]}

Out[13]= {Y[X[1]], Y[X[2]], Y[X[3]], Y[X[4]]}

Y wird über die Elemente der Liste gemapped.

Wenn Y mehrere Argumente hat,


In[14]:= Y[peter, #, klamser] & /@ {X[1], X[2], X[3], X[4]}

Out[14]= {Y[peter, X[1], klamser], Y[peter, X[2], klamser], Y[peter, X[3], klamser], Y[peter, X[4], klamser]}


Sehen Sie? Es lohnt sich auch, die "Neat Examples" anzuschauen, die R. Ference und R. E. Eference bereithalten, z.B. bei Slot


In[15]:= faculty = If[#1 == 1, 1, #1 #0[#1 - 1]] &

Out[15]= If[#1 == 1, 1, #1 #0[#1 - 1]] &

In[16]:= faculty[10]

Out[16]= 3628800


Sie sehen eine Selbstreferenz (#0) und Sie fragen sich sofort, ob es auch ander geschrieben werden kann:


In[17]:= Clear[faculty]
faculty = If[#1 == 1, 1, #1 faculty[#1 - 1]] &

In[19]:= faculty[10]

Out[19]= 3628800


kann es. Geben Sie keine negative Zahl an faculty. Das ist eine Schwäche der "Neat Examples" (der Benutzer muss mitdenken), also narrensicher etwa

In[20]:= Clear[faculty]
faculty = If[#1 == 1, 1, If[#1 < 1, #1, #1 #0[#1 - 1]]] &

Out[21]= If[#1 == 1, 1, If[#1 < 1, #1, #1 #0[#1 - 1]]] &

In[22]:= faculty[10]

Out[22]= 3628800

In[23]:= faculty[-10]

Out[23]= -10


ist immer noch nicht narrensicher, weil das Argument nicht auf Ganzzahligkeit geprüft wird, dann also

In[24]:= Clear[faculty]
faculty =
 If[#1 == 1, 1, If[#1 < 1 \[Or] ! IntegerQ[#1], #1, #1 #0[#1 - 1]]] &

Out[25]= If[#1 == 1, 1,
  If[#1 < 1 || ! IntegerQ[#1], #1, #1 #0[#1 - 1]]] &

In[26]:= faculty[12]

Out[26]= 479001600

In[27]:= faculty[-12]

Out[27]= -12

In[28]:= faculty[\[Pi]]

Out[28]= \[Pi]


na gut & Grüsse

Udo.



Am 12.10.2020 um 19:54 schrieb Peter Klamser via demug:
Hallo,

warum gibt es bei der Frage an MMA:
#["Name"] & /@ IsotopeData["U238", "BranchingRatios"]
die Antwort
{1.00[Name],5.45*10^-7[Name],2.2*10^-12[Name]}?

Kann ich eleganter auf den Wert zugreifen als mit
#[[0]] & /@ (#["Name"] & /@ IsotopeData["U238", "BranchingRatios"])
{1.00, 5.45*10^-7, 2.2*10^-12}?

fragt Peter & wünscht eine sehr gute Woche :-)
_______________________________________________
DMUG Deutschsprachiges Mathematica-Forum demug@XXXXXXX.ch
http://www.mathematica.ch/mailman/listinfo/demug
Archiv: http://www.mathematica.ch/archiv.html
_______________________________________________
DMUG Deutschsprachiges Mathematica-Forum demug@XXXXXXX.ch
http://www.mathematica.ch/mailman/listinfo/demug
Archiv: http://www.mathematica.ch/archiv.html

Verweise:
Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

DMUG DMUG-Archiv, http://www.mathematica.ch/archiv.html