Task

Display a finite linear combination in an infinite vector basis 



(

e

1


,

e

2


,
…
)


{\displaystyle (e_{1},e_{2},\ldots )}

.

Write a function that, when given a finite list of scalars 



(

α

1


,

α

2


,
…
)


{\displaystyle (\alpha ^{1},\alpha ^{2},\ldots )}

, 
creates a string representing the linear combination 




∑

i



α

i



e

i




{\displaystyle \sum _{i}\alpha ^{i}e_{i}}

 in an explicit format often used in mathematics, that is:






α


i

1





e


i

1




±

|


α


i

2





|


e


i

2




±

|


α


i

3





|


e


i

3




±
…


{\displaystyle \alpha ^{i_{1}}e_{i_{1}}\pm |\alpha ^{i_{2}}|e_{i_{2}}\pm |\alpha ^{i_{3}}|e_{i_{3}}\pm \ldots }


where 




α


i

k




≠
0


{\displaystyle \alpha ^{i_{k}}\neq 0}







The output must comply to the following rules:

   don't show null terms, unless the whole combination is null. 
 e(1)     is fine,     e(1) + 0*e(3)     or     e(1) + 0     is wrong.
   don't show scalars when they are equal to one or minus one. 
 e(3)     is fine,     1*e(3)     is wrong.
   don't prefix by a minus sign if it follows a preceding term.   Instead you use subtraction. 
 e(4) - e(5)     is fine,     e(4) + -e(5)     is wrong.


Show here output for the following lists of scalars:

 1)    1,  2,  3
 2)    0,  1,  2,  3
 3)    1,  0,  3,  4
 4)    1,  2,  0
 5)    0,  0,  0
 6)    0
 7)    1,  1,  1
 8)   -1, -1, -1
 9)   -1, -2,  0, -3
10)   -1



; Process and output values.
Procedure WriteLinear(Array c.i(1))
  Define buf$,
         i.i, j.i, b,i
 
  b = #True
  j = 0
 
  For i = 0 To ArraySize(c(), 1) 
    If c(i) = 0 : Continue : EndIf
 
    If c(i) < 0
      If b : Print("-") : Else : Print(" - ") : EndIf
    ElseIf c(i) > 0
      If Not b : Print(" + ") : EndIf
    EndIf
 
    If c(i) > 1
      Print(Str(c(i))+"*")
    ElseIf c(i) < -1
      Print(Str(-c(i))+"*")
    EndIf
 
    Print("e("+Str(i+1)+")")
 
    b = #False
    j+1
  Next
 
  If j = 0 : Print("0") : EndIf
  PrintN("")
EndProcedure
 
 
Macro VectorHdl(Adr_Start, Adr_Stop)
  ; 1. Output of the input values  
  Define buf$ = "[", *adr_ptr
  For *adr_ptr = Adr_Start To Adr_Stop - SizeOf(Integer) Step SizeOf(Integer)
    buf$ + Str(PeekI(*adr_ptr))
    If *adr_ptr >= Adr_Stop - SizeOf(Integer)
      buf$ + "]  ->  "
    Else
      buf$ + ", "
    EndIf    
  Next
  buf$ =  RSet(buf$, 25)
  Print(buf$)  
 
  ; 2. Reserve memory, pass and process values.
  Dim a.i((Adr_Stop - Adr_Start) / SizeOf(Integer) -1)
  CopyMemory(Adr_Start, @a(0), Adr_Stop - Adr_Start)
  WriteLinear(a())
EndMacro
 
 
If OpenConsole("")
  ; Pass memory addresses of the data.
  VectorHdl(?V1, ?_V1)
  VectorHdl(?V2, ?_V2)
  VectorHdl(?V3, ?_V3)
  VectorHdl(?V4, ?_V4)
  VectorHdl(?V5, ?_V5)
  VectorHdl(?V6, ?_V6)
  VectorHdl(?V7, ?_V7)
  VectorHdl(?V8, ?_V8)
  VectorHdl(?V9, ?_V9)
  VectorHdl(?V10, ?_V10)    
 
  Input()
EndIf
 
End 0
 
 
DataSection
  V1:
  Data.i 1,2,3
  _V1:
  V2:
  Data.i 0,1,2,3
  _V2:
  V3:
  Data.i 1,0,3,4
  _V3:
  V4:
  Data.i 1,2,0
  _V4:
  V5:
  Data.i 0,0,0
  _V5:
  V6:
  Data.i 0
  _V6:
  V7:
  Data.i 1,1,1
  _V7:
  V8:
  Data.i -1,-1,-1
  _V8:
  V9:
  Data.i -1,-2,0,-3
  _V9:
  V10:
  Data.i -1
  _V10:  
EndDataSection