Opened 3 years ago

# sum/product of immutable matrices is not immutable

Reported by: Owned by: dkrenn major sage-8.1 linear algebra matrix galipnik N/A

### Description

```sage: M = Matrix([[1,2],[3,4]]); M.set_immutable()
sage: N = Matrix([[2,2],[4,4]]); N.set_immutable()
sage: (M + N).is_immutable()
False
sage: (M * N).is_immutable()
False
```

This is counter-intuitive. The result should again be immutable.

### comment:2 follow-up: ↓ 3 Changed 3 years ago by jdemeyer

For reference, this is how Python deals with it (`frozenset` being an immutable version of `set`):

```>>> frozenset([1,2,3]) | frozenset([4,5,6])
frozenset({1, 2, 3, 4, 5, 6})
>>> frozenset([1,2,3]) | set([4,5,6])
frozenset({1, 2, 3, 4, 5, 6})
>>> set([1,2,3]) | frozenset([4,5,6])
{1, 2, 3, 4, 5, 6}
>>> set([1,2,3]) | set([4,5,6])
{1, 2, 3, 4, 5, 6}
```

So the mutability of `A | B` is taken from the mutability of `A`.

### comment:3 in reply to: ↑ 2 Changed 3 years ago by mcognetta

For reference, this is how Python deals with it (`frozenset` being an immutable version of `set`):

```>>> frozenset([1,2,3]) | frozenset([4,5,6])
frozenset({1, 2, 3, 4, 5, 6})
>>> frozenset([1,2,3]) | set([4,5,6])
frozenset({1, 2, 3, 4, 5, 6})
>>> set([1,2,3]) | frozenset([4,5,6])
{1, 2, 3, 4, 5, 6}
>>> set([1,2,3]) | set([4,5,6])
{1, 2, 3, 4, 5, 6}
```

So the mutability of `A | B` is taken from the mutability of `A`.

Is this the desired behavior or should it be that if either is immutable than the resultant should be as well? For example, this is how networkx handles a graph product of a undirected and directed graph (in either order).

Note: See TracTickets for help on using tickets.