PRODUCTS
Products Overview
Mathematica
Mathematica for Students
Mathematica Home Edition
Wolfram
CDF Player
(free download)
Computable Document Format (CDF)
web
Mathematica
grid
Mathematica
Wolfram
Workbench
Mathematica
Add-Ons
Wolfram|Alpha Products
SOLUTIONS
Solutions Overview
Engineering
Aerospace Engineering & Defense
Chemical Engineering
Control Systems
Electrical Engineering
Image Processing
Industrial Engineering
Materials Science
Mechanical Engineering
Operations Research
Optics
Petroleum Engineering
Biotechnology & Medicine
Bioinformatics
Medical Imaging
Finance, Statistics & Business Analysis
Actuarial Sciences
Data Analysis & Mining
Econometrics
Economics
Financial Engineering & Mathematics
Financial Risk Management
Statistics
Software Engineering & Content Delivery
Authoring & Publishing
Interface Development
Software Engineering
Web Development
Science
Astronomy
Biological Sciences
Chemistry
Environmental Sciences
Geosciences
Social & Behavioral Sciences
Design, Arts & Entertainment
Game Design, Special Effects & Generative Art
Education
STEM Education Initiative
Higher Education
Community & Technical College Education
Primary & Secondary Education
Students
Technology
Computable Document Format (CDF)
High-Performance & Parallel Computing (HPC)
See Also: Technology Guide
PURCHASE
Online Store
Other Ways to Buy
Volume & Site Licensing
Contact Sales
Software
Service
Upgrades
Training
Books
SUPPORT
Support Overview
Knowledge Base
Learning Center
Community & Forums
Training & Free Seminars
Does My Site Have a License?
Wolfram User Portal
COMPANY
About Wolfram Research
News & Events
Wolfram Blog
Partnerships
Employment Opportunities
History of
Mathematica
Stephen Wolfram's Home Page
Contact Us
OUR SITES
All Sites
Wolfram|Alpha
Demonstrations Project
MathWorld
Integrator
Wolfram Functions Site
Mathematica Journal
Wolfram Media
Wolfram
Tones
Wolfram Science
Stephen Wolfram
DOCUMENTATION CENTER SEARCH
New to
Mathematica
?
Find your learning path
»
Mathematica
>
Core Language
>
Procedural Programming
>
Scoping Constructs
>
With
>
Mathematica
>
Core Language
>
Package Development
>
Namespace Management
>
Scoping Constructs
>
With
>
BUILT-IN MATHEMATICA SYMBOL
Local Constants
Some General Notations and Conventions
Variables in Pure Functions and Rules
Tutorials »
|
Module
Block
ReplaceAll
See Also »
|
Evaluation Control
Language Overview
Procedural Programming
Scoping Constructs
More About »
With
With
specifies that in
expr
occurrences of the symbols
x
,
y
, ... should be replaced by
,
, ....
MORE INFORMATION
With
allows you to define local constants.
With
replaces symbols in
expr
only when they do not occur as local variables inside scoping constructs.
You can use
With
as the right-hand side of a transformation rule with a condition attached.
With
has attribute
HoldAll
.
With
constructs can be nested in any way, with inner variables being renamed if necessary.
With
is a scoping construct that implements read-only lexical variables.
EXAMPLES
CLOSE ALL
Basic Examples
(1)
In[1]:=
In[2]:=
Out[2]=
Scope
(3)
Use
With
to insert values into held expressions:
The variable names can be the same:
Use a constant for a value that is needed more than once:
Applications
(1)
With
allows inserting values into unevaluated expressions:
Properties & Relations
(4)
Module
introduces local variables to which you can assign values:
With
is faster than
Module
:
Block
localizes values only; it does not substitute values.
Module
creates new symbols:
With
allows substitution inside an unevaluated expression, preserving nested scopes:
Ordinary substitution does not preserve scoping:
Possible Issues
(1)
With
is a scoping construct; variables are renamed in nested scopes:
Build the function from its elements to avoid the renaming:
Neat Examples
(2)
Find a zero of an arbitrary function using Newton's method:
Find a fixed point:
A version of
With
where the initializer is within the scope of the local variable:
Here the
f
inside the function definition is not inside its own scope:
SEE ALSO
Module
Block
ReplaceAll
TUTORIALS
Local Constants
Some General Notations and Conventions
Variables in Pure Functions and Rules
MORE ABOUT
Evaluation Control
Language Overview
Procedural Programming
Scoping Constructs
New in 2