Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4405 > unrolled thread

Re: Unprotect

Started byLeonid Shifrin <lshifr@gmail.com>
First post2011-08-15 00:19 +0000
Last post2011-08-15 00:19 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Unprotect Leonid Shifrin <lshifr@gmail.com> - 2011-08-15 00:19 +0000

#4405 — Re: Unprotect

FromLeonid Shifrin <lshifr@gmail.com>
Date2011-08-15 00:19 +0000
SubjectRe: Unprotect
Message-ID<j29omd$79o$1@smc.vnet.net>
Hi Themis,

In code,  one possible answer to both questions is this:

Entropy;
Unprotect[Entropy];
Entropy[x_] := 1/x;
DownValues[Entropy] = RotateRight[DownValues[Entropy]];
Entropy[10.]

To understand why you need to call Entropy first, and why this solves the
first problem, please read this post:

http://stackoverflow.com/questions/5649379/why-do-i-have-to-evaluate-this-twice/

Note that I don't justify this behavior - IMO it is a borderline bug, or at
least this contradicts the semantics of Protect/Unprotect, as exposed to the
user.

The reason why manipulations with DownValues solve the second problem (error
message) can be seen upon removing the ReadProtected attribute of Entropy
and inspecting the code. It uses the construct like

f[args___]:=Block[{someValue = ...}, someCode/;someValue =!=$Failed]

This looks like a general definition (since triple underscore is used), but
it is not, since the condition inside Block is a part of the pattern. When
you add your definition, it is therefore not considered more specific by
Mathematica, and is therefore added after the built-in one. It is the
built-in definition that is responsible for the error message. By
rearranging DownValues, we put your definition on top, so the built-in never
fires. I would consider  it a possible implementation defect - one could
instead add another built-in definition  like

f[args___]:="never happens"/;Message[...]

and issue a message here - and this problem would then be absent.

Regards,
Leonid


On Sun, Aug 14, 2011 at 5:15 AM, Themis Matsoukas <tmatsoukas@me.com> wrote:

> I want to redefine a build in command. I use the following example:
>
> Unprotect[Entropy];
> Entropy[x_] := 1/x
> Entropy[10.]
>
> When I execute these commands on a fresh kernel I get:
>
> SetDelayed::write: Tag Entropy in Entropy[x_] is Protected. >>
>
> $Failed
>
> Entropy::targ: Argument 10.` at position 1 should be a List, a SparseArray,
> or a String. >>
>
> Entropy[10.]
>
> If I execute a second time I get my definition to work but I still get a
> warning about the arguments:
>
> Entropy::targ: Argument 10.` at position 1 should be a List, a SparseArray,
> or a String. >>
>
> 0.1
>
> So, two questions:
> 1. Do I have to execute twice to cause a custom definition to stick?
> 2. How do I get rid of the warning once the custom definition works?
>
> Running 8.01 on Lion.
>
> {"8.0 for Mac OS X x86 (64-bit) (February 24, 2011)", 1}
>
> Themis
>
>

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web