Opened 13 years ago

Closed 11 years ago

# matlab matrix conversion issue

Reported by: Owned by: mabshoff mhansen major sage-4.5.2 misc matlab conversion sage-4.5.2.alpha1 Mike Hansen Ross Kyprianou N/A

### Description

Reported by Kate:

```a0 = matlab('eye(50)')
sage: a1 = matrix(ZZ,a0)
---------------------------------------------------------------------------
<type 'exceptions.TypeError'>             Traceback (most recent call last)

/home/mabshoff/<ipython console> in <module>()

/home/was/s/local/lib/python2.5/site-packages/sage/matrix/constructor.py in matrix(arg0, arg1, arg2, arg3, sparse)
287
288     if hasattr(arg1, '_matrix_'):
--> 289         return arg1._matrix_(arg0)
290
291     if arg0 is None:

/home/was/s/local/lib/python2.5/site-packages/sage/interfaces/matlab.py in _matrix_(self, R)
283         M = MatrixSpace(R, nrows, ncols)
284         v = sum([[x for x in w.split()] for w in v], [])
--> 285         return M(v)
286
287     def set(self, i, j, x):

/home/was/s/local/lib/python2.5/site-packages/sage/matrix/matrix_space.py in __call__(self, entries, coerce, copy, rows)
352             return self(entries.matrix(), copy=False)
353
--> 354         return self.matrix(entries, copy=copy, coerce=coerce, rows=rows)
355
356     def change_ring(self, R):

/home/was/s/local/lib/python2.5/site-packages/sage/matrix/matrix_space.py in matrix(self, x, coerce, copy, rows)
965                 x = new_x
966
--> 967         return self.__matrix_class(self, entries=x, copy=copy, coerce=coerce)
968
969     def matrix_space(self, nrows=None, ncols=None, sparse=False):

/home/mabshoff/matrix_integer_dense.pyx in sage.matrix.matrix_integer_dense.Matrix_integer_dense.__init__()

<type 'exceptions.TypeError'>: entries has the wrong length
```

Cheers,

Michael

### comment:1 Changed 13 years ago by mabshoff

Moreover there is also the following issue reported in the same thread:

```a00 = str(mathlab('mat2str(eye(50))'))
a01 = a00.replace(' ',',').replace(';','],[')
a02 = '[' + a01 = ']'
a03 = eval(a02)
a1 = matrix(zz,a03)
a2 = matlab.sage2matlab_matrix_string(a1)
a3 = 'm1 = ' + a2
matlab.eval(a3)     # crashes
# Q: caused by "maxread=100" and/or "eval_using_file_cutoff=100"
# in devel/sage-main/build/sage/interfaces/matlab.py ?
```

Cheers,

Michael

### comment:2 Changed 12 years ago by mhansen

• Owner changed from cwitty to mhansen
• Status changed from new to assigned

The original bug is caused by things like 'Columns', '27', 'through', '39' appearing in the entries.

### comment:3 Changed 11 years ago by mhansen

• Report Upstream set to N/A
• Status changed from new to needs_review

### comment:4 Changed 11 years ago by rossk

• Authors set to Mike Hansen
• Reviewers set to Ross Kyprianou
• Status changed from needs_review to needs_work

Tried this on 2 platforms. First example is fixed by the patch i.e.

```b0 = matlab('eye(50)')
b1 = matrix(ZZ,b0)
```

but the second example (note that 3 minor typos have been removed) is still broken but shouldnt be (i.e. the displaying/printing of variable a3 shows that a3 is a valid matlab expression). Is the reference to the \x07 character mean "BELL" (a 'ding' made by PCs)? Note that if the following code uses eye(5) it works but with eye(6) it crashes with that x07 error.

```sage: a00 = str(matlab('mat2str(eye(6))'))
sage: a01 = a00.replace(' ',',').replace(';','],[')
sage: a02 = '[' + a01 + ']'
sage: a03 = eval(a02)
sage: a1 = matrix(ZZ,a03)
sage: a2 = matlab.sage2matlab_matrix_string(a1)
sage: a3 = 'm1 = ' + a2
sage: matlab.eval(a3)
'\x07??? source("/home/rossk/.sage//temp/sage.math.washington.edu/31533//interface//tmp31533");\n           |\nError: The input character is not valid in MATLAB statements or expressions.\n'
sage: a3
'm1 = [1, 0, 0, 0, 0, 0; 0, 1, 0, 0, 0, 0; 0, 0, 1, 0, 0, 0; 0, 0, 0, 1, 0, 0; 0, 0, 0, 0, 1, 0; 0, 0, 0, 0, 0, 1]'
```

### comment:5 Changed 11 years ago by mhansen

• Status changed from needs_work to needs_review

I've posted a new patch which fixes the issue above.

### comment:6 Changed 11 years ago by rossk

• Status changed from needs_review to positive_review

Mike

Looks good. The patch fixes the \x07 issue and the matrices that crashed because they ran over multiple pages (if anything else is discovered with the matlab interface, it should go into a new ticket). Positive review.

### comment:7 Changed 11 years ago by ddrake

• Merged in set to sage-4.5.2.alpha1
• Resolution set to fixed
• Status changed from positive_review to closed

### comment:8 Changed 10 years ago by mpatel

Please see #9608 for docbuild warnings that may stem from this ticket.

Note: See TracTickets for help on using tickets.