Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Negative numbers are not taken into consideration by mathematica's built-in functions.

FromDigits["-2"]

Gives me this message:

FromDigits::nlst: The expression -2 is not a list of digits or a string of valid digits. >>

Also:

In[2]:=IntegerString[-2]

Gives:

Out[2]:="2"

I was wondering if someone had some code to do this. I'm currently making a small program in mma which will help me enter matrices manually because it's cumbersome to type in every single ,{} for each row/column. Here's what I had in mind:

enterMat[x_, y_] := MatrixForm[ReleaseHold[
    Table[Hold[FromDigits[InputString[]]], {x}, {y}]]]

Gives me a series of popups in which I enter the value 1 by 1 pressing enter after each to pass to the next value. It's faster and more comfortable to do it this way since I can use the numpad instead. My only problem is that it just doesn't work with negative numbers!

If you have a solution or you know a great way to enter/scan/manipulate matrices, I would like to hear it. Thanks!

share|improve this question
    
I think ToString and ToExpression are what you are looking for. As for your matrix-input, you might look at this question –  Albert Retey Feb 6 at 15:24
1  
... and of course this questions and the corresponding answers might also be of interest... –  Albert Retey Feb 6 at 15:27
    
A negative integer is not a digit! For a given base b, the digits are 0, 1, 2, ..., b - 1. –  murray Apr 20 at 20:23

2 Answers 2

This question touches on a lot of things and I am not sure which elements to focus on. I guess I'll take it in reverse order, as you state that the reason for the question is:

I'm currently making a small program in mma which will help me enter matrices manually because it's cumbersome to type in every single ,{} for each row/column.

Yes, that is cumbersome, and fortunately it's not what you're supposed to do. Instead please read: Entering Tables and Matrices and start entering your data that way.

For conversion of strings themselves please see How do you convert a string containing a number in C scientific notation to a Mathematica number? There are a number of methods you might select between depending on the application, such as StringToDouble, Read, ReadList, ImportString, and ParseTable. If after reviewing the tutorial above you still have an application for these functions please describe it and I shall make a recommendation.

share|improve this answer
up vote 0 down vote accepted

Here's what I decided to use finally, based on the code of Faysal Aberkane in How to visualize/edit a big matrix as a table? and Theo Tiger from Excel column label from integer

ToExcelCol[n_Integer] := 
 Module[{num, base}, base = Floor[Log[26, 25 (n + 1)]];
  num = n - Total[Power[26, #] & /@ (Range[base] - 1)];
  StringJoin@
   PadLeft[FromCharacterCode /@ (IntegerDigits[num, 26] + 65), base, 
    "A"]]

SetAttributes[GetSymbolName, HoldFirst];
GetSymbolName[symbol_] := ToString@HoldForm@symbol;

SetAttributes[editMatrix, HoldFirst];
Options[editMatrix] = {"MaxFields" -> {10, 5}, 
   "ExcelColumnLabel" -> True};
editMatrix[m_, OptionsPattern[]] := 
  With[{maxrows = Min[OptionValue["MaxFields"][[1]], Length[m]], 
    maxcols = Min[OptionValue["MaxFields"][[2]], Length[m[[1]]]], 
    excelColumnLabel = OptionValue["ExcelColumnLabel"]}, 
   Interpretation[
    Panel[DynamicModule[{rowoffset = 0, coloffset = 0}, 
      Grid[{{Style[GetSymbolName[m], Bold]}, {Grid[
          Table[With[{x = i, y = j}, 
            Switch[{i, j}, {0, 0}, Spacer[0], {0, _}, 
             Dynamic[
              If[excelColumnLabel, ToExcelCol[y + coloffset], 
               y + coloffset]], {_, 0}, Dynamic[x + rowoffset], _, 
             InputField[Dynamic[m[[x + rowoffset, y + coloffset]]], 
              FieldSize -> 3]]], {i, 0, maxrows}, {j, 0, maxcols}], 
          Spacings -> {0, 0}], 
         If[Length@m > maxrows, 
          VerticalSlider[
           Dynamic[rowoffset], {Length[m] - maxrows, 0, 1}]]}, {If[
          Length@First@m > maxcols, 
          Slider[Dynamic[coloffset], {0, Length[m[[1]]] - maxcols, 
            1}]]}}]]], m]];

a matrix

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.