Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.2K Views
|
13 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Why does this variable forget its assignment?

Why does $\hat{\beta}$ change values? If possible, I'd like to use math notation for clarity in code like this simplified example.

Attachments:
POSTED BY: Jay Gourley
13 Replies
Posted 7 months ago

Eric Rimbey's post led me to duplicate a related unexpected result. Notice the unexpected variable assignment self-corrects after being executed for beta but not for alpha. I was not aware of this self-correcting mechanism. It might explain why unexpected results appear then disappear.

POSTED BY: Jay Gourley
Posted 7 months ago

There's no self-correcting mechanism (or maybe I don't understand what you meant).

OverHat[\[Beta]] = 7
(* This created a DownValues entry for OverHat[\[Beta]], where \[Beta] is the literal symbol \[Beta]. *)

OverHat[\[Beta]] = 1
(* This overwrites the DownValues for OverHat[\[Beta]]. *)

\[Beta] = 5
(* This creates OwnValues for \[Beta]. This has no effect on the DownValues for OverHat. *)

Style[OverHat[\[Beta]], Red]
(* The OverHat[\[Beta]] evaluates to OverHat[5]. There are no `DownValues` for OverHat[5], so it just remains as OverHat[5] and the Style is applied. *)

OverHat[\[Beta]] = 7
(* This creates DownValues for OverHat[5]. *)

OverHat[\[Beta]] = 1
(* This overwrites the DownValues for OverHat[5]. *)

\[Beta] = 5
(* This overwrites the OwnValues for \[Beta]. It happens to be the same value. *)

Style[OverHat[\[Beta]], Red]
(* OverHat[\[Beta]], using DownValues, evaluates to OverHat[5] and then, using DownValues again, to 1. Then Style is applied. *)

You can walk through the \[Alpha]-related expressions in the same way. Just be aware that you've changed the order, starting with assigning OwnValues to \[Alpha] before assigning DownValues to OverHat.

You can evaluate OwnValues[\[Beta]] and DownValues[OverHat] after each assignment to see the current state of the "environment".

POSTED BY: Eric Rimbey

Thanks again, Eric Rimbey. It's good to know how those names are handled. I had to convince myself I was not hallucinating when they returned strange values, then returned exactly what I expected a second later. By the way, that's what I meant by "self-correcting". Sorry if that description was too cute.

POSTED BY: Jay Gourley
Posted 7 months ago

Let's make this more focused. Make sure you've cleared all definitions (or restarted the kernel).

OverHat[\[Beta]] = "value for overhat beta";
OverHat[\[Beta]]
(* "value for overhat beta" *)

\[Beta] = "value for just beta";
OverHat[\[Beta]]
(* OverHat["value for just beta"] *)

OverHat[\[Beta]] = "value for \"value for just beta\"";
OverHat[\[Beta]]
(* "value for \"value for just beta\"" *)

OverHat[Unevaluated[\[Beta]]]
(* "value for overhat beta" *)

Information[OverHat, "Definitions"]
(* OverHat["value for just beta"] = "value for \"value for just beta\""
    OverHat[\[Beta]] = "value for overhat beta" *)
POSTED BY: Eric Rimbey

If you are interested in using both beta and beta-hat as independent symbols, let me recommend the Notations package, http://reference.wolfram.com/language/Notation/guide/NotationPackage.html

As Eric pointed out, a definition Overhat[\[Beta]] = ... depends on \[Beta] being undefined or never changing its definition. In other words, it is not independent of \[Beta]. Thus it and \[Beta] cannot be used as independent variables.

POSTED BY: Michael Rogers

Thanks, Michael Rogers for the link. It contains another link to tech notes with detailed explanations of the key functions. Lot's of cool features there that can make code even more accessible than Mathematica's already accessible convention of using real English words with meanings to identify its built-in functions and arguments.

POSTED BY: Jay Gourley
Posted 7 months ago

When I try, I also get errors in the following way. I started to put Hold[] around each expression and as I did that the editor went completely haywire, the hats moved around, were over the wrong symbols, and so on. The cell got weird. I opened the file in BBEdit and everything looked "normal" in that there were no extra symbols or so in the expressions. Running 14.2 on Apple M1 Macbook.

POSTED BY: Paul Nielan
Posted 7 months ago

Just for the record, I cannot reproduce the problem at all (14.2.0 on MacOS).

POSTED BY: Eric Rimbey
Posted 7 months ago

Thanks, Eric Rimbey, for checking. Strangely, I sometimes cannot reproduce it either. When I started this thread, I thought it was consistently reproducible. I now know it occurs sometimes and sometimes doesn't. Unless you believe in software spirits, something I'm doing triggers it. Maybe Michael Rogers (also in this thread) did the same thing. If I figure out what I'm doing that turns this oddity off and on, I'll add that to this thread. If you have ideas, please share. In the meantime, here are two iterations of the same notebook I posted before. No changes were made between the two other than reloading and executing. It's likely another open notebook at the time had altered the kernel for one and not the other.

Attachments:
POSTED BY: Jay Gourley
Posted 7 months ago
POSTED BY: Eric Rimbey

Weird: The first time I evaluated it, I got the buggy results. Then I copied some of the pieces and evaluated them separately. The alpha line threw a syntax error. Then I deleted my mess, and reevaluated the four-line cell. Then it worked fine. I quit the kernel and tried again. It worked fine. Maybe there's a parsing error in the original notebook. I didn't edit the first cell, but maybe it got re-parsed correctly.

It won't let me add this notebook: https://www.wolframcloud.com/obj/mroge02/Published/198902202

POSTED BY: Michael Rogers

I suspect you’ll get the expected result shown if you evaluate in separate cells. What you are seeing, I think, is a display error but (I hope) not an error in the actual assignment.

POSTED BY: Daniel Lichtblau
POSTED BY: Jay Gourley
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard