All Sites & Public Resources...
Products & Services
Data Science Platform
Products for Education
Wolfram|Alpha for Mobile
Course Assistant Apps
Paid Project Support
All Products & Services »
Revolutionary knowledge-based programming language.
Central infrastructure for Wolfram's cloud products & services.
Technology-enabling science of the computational universe.
Computable Document Format
Computation-powered interactive documents.
Software engine implementing the Wolfram Language.
Wolfram Natural Language Understanding System
Knowledge-based broadly deployed natural language.
Wolfram Data Framework
Semantic framework for real-world data.
Wolfram Universal Deployment System
Instant deployment across cloud, desktop, mobile, and more.
Curated computable knowledge powering Wolfram|Alpha.
All Technologies »
Aerospace & Defense
All Solutions for Education
Web & Software
Authoring & Publishing
Finance, Statistics & Business Analysis
Financial Risk Management
Internet of Things
All Solutions »
Support & Learning
Find an Answer
Ask for Help
Post a Question
Videos & Screencasts
Conferences & Seminars
All Support & Learning »
Work with Us
A New Kind of Science
Wolfram + Raspberry Pi
Wolfram Innovator Awards
All Company »
Mark as an Answer
Mathematica evaluates numerical functions symbolically. How can I stop it?
The attached file demo.nb:
Defines two Bezier curves cur1 and cur2. It graphs them, and then tries to use NMaximize to find the distance between them.
As the results show
(1) Plot first evaluates the plotted function with the symbolic argument "t", even though the documentation of plot says " ...evaluates the fi and gi only after assigning specific numerical values to variables." Plot seems to be smart enough to produce a correct plot, even though the call with "t" returns Null.
(2) Then NMaximize also tries to evaluate the target function with the symbolic argument "t". However, NMaximize isn't smart enough to recover when the target function returns nonsense. (And I have other examples in which the symbolic call hopelessly messes up the state of the target function, so that _no_ future call with any kind of argument would return a meaningful result.)
This seems like a general behavior in Mathematica ... I've seen it in other places.
In the case of NMaximize, it seems particularly inappropriate. What does the "N" mean, if NMaximize tries to evaluate the target symbolically?
I'm sure that the Wolfram people have a reason for this. But it seems to me that (1) the documentation should describe this behavior, and (2) there should be an option to turn it off.
And, of course, Plot should work as documented.
My apolgies for not putting the code in here directly, and making you look at the attached file. The machine I have Mathematica on is not connected to the net, so I have to carry home on a thumb drive anything I post here. So it's hard to post code.
Wolfram should be aware that it takes an hour to put togther this clean demo. And _days_ wasted because of this kind of non-intuitive and non-documented behavior of Mathematica.
I couldn't see any file attached, so I'll comment on NMaximize evaluation in general.
Where necessary, for many numerical functions in Mathematica, there is a statement in the documentation, for example "FindMinimum has attribute HoldAll ... FindMinimum first localizes the values of all variables, then evaluates f with the variables being symbolic, and then repeatedly evaluates the result numerically."
Such a statement is not needed for NMaximize, because it does not have any Hold* attributes. By the general evaluation principles of Mathematica, see the
, this means that any and all arguments will be evaluated before NMaximize even sees them.
As already pointed out in this
, there is a simple way to define the objective function by restricting it so it evaluates only for numerical arguments, using the _?NumericQ pattern test on the variables. You may also find this Technical support
It might be a good idea to organize the computations so that your target function will never "return nonsense" or get in a hopelessly messed-up state so that "_no_ future call with any kind of argument would return a meaningful result". Doing that, however, doesn't seem to be a task for NMaximize.
When you define a numerical function that can't be evaluated symbolically, you should define in the form f[ x_?NumberQ] := ...
Many of Mathematica's numerical algorithms do some symbolic evaluation to improve their performance. _?NumberQ prevents that from happening.
Expanded version of
Be respectful. Review our
to understand your role and responsibilities.
Data Science Platform
Volume & Site Licensing
Wolfram Language Documentation
Connected Devices Project
Wolfram Data Drop
Wolfram + Raspberry Pi
© 2015 Wolfram. All rights reserved.
Learn how »
Translate this page: