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


Groups > comp.soft-sys.math.mathematica > #3265

Re: Assigning part of indexed object

From Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Assigning part of indexed object
Date 2011-06-23 11:27 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <itv7ve$pbu$1@smc.vnet.net> (permalink)
References <itsjn7$8u7$1@smc.vnet.net>

Show all headers | View raw


Arguably Set should be a bit more intelligent about this situation.
But it is an easy fix:

Unprotect[Set];
Set[sym_[[part_]], val_] := sym = ReplacePart[sym, part -> val];
Protect[Set];

In[65] := m[1] = Range[5]; m[1][[1]] = 10
Out[66] = {10,2,3,4,5}

Bear in mind that since this relies on pattern matching, it will not
work in situations
where pattern matching is not available via the interpreter (for
example, in compiled
code).

On Jun 22, 12:29 pm, "Fabrice P. Laussy" <fabrice.lau...@n0spam-
gmail.com> wrote:
> How do you set the part of an indexed object, say:
>
> m[1] = Range[5];
>
> is my indexed object (I could have m[2], etc.). Now I
> want to do something like:
>
> m[1][[1]] = 10;
>
> with effect of having m[1] read:
>
> {10, 2, 3, 4, 5}
>
> but of course the line above evaluates to 1 = 10.
>
> This could be achieved by having a non-indexed object get
> in the way, e.g., m1=m[1], assign m1 and then return to
> m[1]=m1, but I believe there should a more elegant way.

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Assigning part of indexed object "Fabrice P. Laussy" <fabrice.laussy@n0spam-gmail.com> - 2011-06-22 11:29 +0000
  Re: Assigning part of indexed object Valeri Astanoff <astanoff@gmail.com> - 2011-06-23 11:25 +0000
  Re: Assigning part of indexed object magma <maderri2@gmail.com> - 2011-06-23 11:26 +0000
  Re: Assigning part of indexed object Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-23 11:27 +0000
    Re: Assigning part of indexed object "Fabrice P. Laussy" <fabrice.laussy@n0spam-gmail.com> - 2011-06-24 11:52 +0000
      Re: Assigning part of indexed object Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-25 09:31 +0000
  Re: Assigning part of indexed object Leonid Shifrin <lshifr@gmail.com> - 2011-06-25 09:30 +0000
    Re: Assigning part of indexed object Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-26 10:31 +0000
    Re: Assigning part of indexed object Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-26 21:40 +0000
  Re: Assigning part of indexed object Leonid Shifrin <lshifr@gmail.com> - 2011-06-29 21:31 +0000
    Re: Assigning part of indexed object "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-07-01 00:46 +0000

csiph-web