How do we make a negative integer out of a string of a negative number and vice-versa?

3

1

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!

Frank

Posted 2014-02-06T15:09:54.987

Reputation: 111

I think ToString and ToExpression are what you are looking for. As for your matrix-input, you might look at this question

– Albert Retey – 2014-02-06T15:24:55.183

1

... and of course this questions and the corresponding answers might also be of interest...

– Albert Retey – 2014-02-06T15:27:04.210

A negative integer is not a digit! For a given base b, the digits are 0, 1, 2, ..., b - 1. – murray – 2014-04-20T20:23:03.137

Answers

0

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

Frank

Posted 2014-02-06T15:09:54.987

Reputation: 111

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.

Mr.Wizard

Posted 2014-02-06T15:09:54.987

Reputation: 259 163