5
$\begingroup$

Please consider the following:

InputField[Dynamic[X1], Number, Alignment -> Center];
InputField[Dynamic[X2], Number, Alignment -> Center];
Slider[Dynamic[Y1], {0, 200, 1}];
Slider[Dynamic[Y2], {0, 200, 1}];
Dynamic[FK, Dynamic[X1*(1 + Y1/100)]];
Dynamic[MK, Dynamic[X2*(1 + Y2/100)]];
Dynamic[HK, Dynamic[FK + MK]];
HeadTest = NumberQ[#] & /@ {X1, X2, Y1, Y2, FK, MK, HK}

Why would FK,MK and HK not have the head Number? (I guess I just did not understand how to use Dynamic and DynamicModule.)

$\endgroup$
2
  • 2
    $\begingroup$ You do not need to create a pure function (# &) to Map (/@) an existing one: NumberQ[#] & /@ is not needed, NumberQ /@ will suffice. $\endgroup$
    – Mr.Wizard
    Commented Aug 17, 2012 at 10:33
  • $\begingroup$ Related: (1864), (2972), (5817) $\endgroup$
    – Mr.Wizard
    Commented Sep 2, 2013 at 7:21

1 Answer 1

9
$\begingroup$

For any beginner these are a must:

Because for your code as it is right now, with no additional definitions, those variables are symbols - you did not define them numerically (Dynamic got nothing to do with it).

X1 // Head

Symbol

Now if you do this you'll get your 'Number':

{X1, X2, Y1, Y2, FK, MK, HK} = Range[7];
InputField[Dynamic[X1], Number, Alignment -> Center];
InputField[Dynamic[X2], Number, Alignment -> Center];
Slider[Dynamic[Y1], {0, 200, 1}];
Slider[Dynamic[Y2], {0, 200, 1}];
Dynamic[FK, Dynamic[X1*(1 + Y1/100)]];
Dynamic[MK, Dynamic[X2*(1 + Y2/100)]];
Dynamic[HK, Dynamic[FK + MK]];
HeadTest = NumberQ[#] & /@ {X1, X2, Y1, Y2, FK, MK, HK}

{True, True, True, True, True, True, True}

I am not sure what you trying to get, but generally the price of dynamic updating is change of Head. Maybe this example will help to understand - turn your attention to what changes, what evaluates and what does not:

{Slider[Dynamic[x]], Dynamic[x], x, Dynamic[x] // Head, x // Head, 
 Sin[x], Sin[Dynamic@x], Dynamic@Sin[x]}

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ "...generally the price of dynamic updating is change of Head." Great insight into and explanation for how to think about dynamic updating. +1 $\endgroup$
    – Jagra
    Commented Aug 17, 2012 at 12:56
  • $\begingroup$ Vitaliy, thanks for identifying the duplicate. Please vote to close the new question as such. $\endgroup$
    – Mr.Wizard
    Commented Sep 2, 2013 at 7:14
  • $\begingroup$ Vitaliy, we get this question often enough that I believe it needs to be added to the pitfalls post. You have the best reviewed answers here on the subject. Would you be willing to write one? If you do please also include whatever part of John Fultz's answer seem appropriate, with attribution. If you do not wish to do this I will try my best. $\endgroup$
    – Mr.Wizard
    Commented Sep 2, 2013 at 7:26
  • $\begingroup$ @Mr.Wizard I would appreciate if you could write this. And yes John's answer of course is super. $\endgroup$ Commented Sep 2, 2013 at 7:30
  • $\begingroup$ Okay, I'll try. When you see it come up (perhaps not tonight) please review and correct it as needed. $\endgroup$
    – Mr.Wizard
    Commented Sep 2, 2013 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.