print_matrix

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

print_matrix

Post by srvaldez »

here's code to print small matrix

Code: Select all

#Include Once "fbc-int/array.bi"

Declare Sub print_matrix Overload(x() As Single, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Double, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Long, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Integer, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Longint, Byref frmt As String)

Sub print_matrix(x() As Single, Byref frmt As String)
   #if __FB_VER_MINOR__ = 7
      Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
   #elseif __FB_VER_MINOR__ >= 8
      Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
   #endif
   If pd->dimensions=1 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         Print Using frmt;x(i);
      Next
      Print
   Elseif pd->dimensions=2 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
            Print Using frmt;x(i,j);
         Next
         Print
      Next
   End If
End Sub
                      
Sub print_matrix(x() As Double, Byref frmt As String)
   #if __FB_VER_MINOR__ = 7
      Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
   #elseif __FB_VER_MINOR__ >= 8
      Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
   #endif
   If pd->dimensions=1 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         Print Using frmt;x(i);
      Next
      Print
   Elseif pd->dimensions=2 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
            Print Using frmt;x(i,j);
         Next
         Print
      Next
   End If
End Sub

Sub print_matrix (x() As Long, Byref frmt As String)
   #if __FB_VER_MINOR__ = 7
      Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
   #elseif __FB_VER_MINOR__ >= 8
      Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
   #endif
   If pd->dimensions=1 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         Print Using frmt;x(i);
      Next
      Print
   Elseif pd->dimensions=2 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
            Print Using frmt;x(i,j);
         Next
         Print
      Next
   End If
End Sub

Sub print_matrix (x() As Integer, Byref frmt As String)
   #if __FB_VER_MINOR__ = 7
      Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
   #elseif __FB_VER_MINOR__ >= 8
      Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
   #endif
   If pd->dimensions=1 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         Print Using frmt;x(i);
      Next
      Print
   Elseif pd->dimensions=2 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
            Print Using frmt;x(i,j);
         Next
         Print
      Next
   End If
End Sub

Sub print_matrix (x() As Longint, Byref frmt As String)
   #if __FB_VER_MINOR__ = 7
      Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
   #elseif __FB_VER_MINOR__ >= 8
      Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
   #endif
   If pd->dimensions=1 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         Print Using frmt;x(i);
      Next
      Print
   Elseif pd->dimensions=2 Then
      For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
         For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
            Print Using frmt;x(i,j);
         Next
         Print
      Next
   End If
End Sub

Dim As Double x(1 To 6, 1 To 6) = {{ 1,  6, 15,  20,  15,   6}, _
                       {1,  7, 21,  35,  35,  21}, _
                       {1,  8, 28,  56,  70,  56}, _
                       {1,  9, 36,  84, 126, 126}, _
                       {1, 10, 45, 120, 210, 252}, _
                       {1, 11, 55, 165, 330, 462 }}
                      
Dim As Long y(0 To 7) = { 11, 22, 33, 44, 55, 99, -12, -13 }
Dim As Integer z(0 To 5) = { 1, 2, 3, 4, 5, 6 }
Dim As LongInt w(-3 To 3) = { 123, 234, 345, 456, 567, 678, 789 }

print_matrix(x(), "#####.###")
Print "-------------------"
print_matrix(y(), "#####.###")
Print "-------------------"
print_matrix(z(), "#####.###")
Print "-------------------"
print_matrix(w(), "#####.###")
output

Code: Select all

    1.000    6.000   15.000   20.000   15.000    6.000
    1.000    7.000   21.000   35.000   35.000   21.000
    1.000    8.000   28.000   56.000   70.000   56.000
    1.000    9.000   36.000   84.000  126.000  126.000
    1.000   10.000   45.000  120.000  210.000  252.000
    1.000   11.000   55.000  165.000  330.000  462.000
-------------------
   11.000   22.000   33.000   44.000   55.000   99.000  -12.000  -13.000
-------------------
    1.000    2.000    3.000    4.000    5.000    6.000
-------------------
  123.000  234.000  345.000  456.000  567.000  678.000  789.000
Last edited by srvaldez on Oct 09, 2021 23:44, edited 2 times in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: print_matrix

Post by dodicat »

Similar, backward compatible.

Code: Select all

#Macro __Arrayptr__(array)
Iif(Ubound(array,0)=1,@array(Lbound(array)), _
Iif(Ubound(array,0)=2,@array(Lbound(array,1),Lbound(array,2)), _
Iif(Ubound(array,0)=3,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3)), _
Iif(Ubound(array,0)=4,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4)), _
Iif(Ubound(array,0)=5,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5)), _
Iif(Ubound(array,0)=6,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6)), _
Iif(Ubound(array,0)=7,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6),Lbound(array,7)), _
Iif(Ubound(array,0)=8,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6),Lbound(array,7),Lbound(array,8)),0))))))))
#endmacro

#macro setptr(datatype,fname)
Function fname(a() As datatype) As datatype Ptr
    Return  __arrayptr__(a)
End Function
#EndMacro

#macro setshow(datatype,fname)
Sub fname(db() As datatype,fmt As String,f As Any Ptr)
    #macro GetNumElements(array,d)
    d=Ubound(array,0)
    For n As Integer=1 To d
        If n=1 Then d=1
        d=d*(Ubound(array,n)-Lbound(array,n)+1)
    Next
    #endmacro
    Dim As Function(() As Typeof(db)) As Typeof(db) Ptr fn=f
    Dim As Typeof(db) Ptr dp=fn(db())
    Var dd=0
    GetNumElements(db,dd)
    Dim As Long c= Ubound(db,1)- Lbound(db,1)+1
    For z As Long=0 To dd-1
        If z Mod c=0  Then Print
        If z Mod Ubound(db,1)*c=0 Then Print
        Print Using fmt; dp[z];
    Next
    Print
End Sub
#endmacro


Dim As Integer y(0 To 7) = { 11, 22, 33, 44, 55, 99, -12, -13 }

Dim As Double x(1 To 6, 1 To 6) = {{ 1,  6, 15,  20,  15,   6}, _
                                   {1,  7, 21,  35,  35,  21}, _
                                   {1,  8, 28,  56,  70,  56}, _
                                   {1,  9, 36,  84, 126, 126}, _
                                   {1, 10, 45, 120, 210, 252}, _
                                   {1, 11, 55, 165, 330, 462 }}

Dim As Double z(1 To 2,1 To 2,1 To 2)={ {{Sqr(1),Sqr(2)},_
                                         {Sqr(3),Sqr(4)}},_
_
                                         {{Sqr(5),Sqr(6)},_
                                         {Sqr(7),Sqr(8)}} }

Dim As String s(...)={"Good","bye",Chr(10),"Press"," any"," key . . ."}

setptr(Integer,integerptr)
setptr(Double,doubleptr)
setptr(String,stringptr)
setshow(Integer,showinteger)
setshow(Double,showdouble)
setshow(String,showstring)



showinteger(y(),"#####",@integerptr)
showdouble(x(),"#####.###",@doubleptr)
showdouble(z(),"#####.##########",@doubleptr)
showstring(s(),"&",@stringptr)
Sleep




 
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: print_matrix

Post by srvaldez »

thank you dodicat :-)
I thought about using a macro, but I have never been much of a macro fan, but it makes sense in this case

Code: Select all

#Include Once "fbc-int/array.bi"

Declare Sub print_matrix Overload(x() As Single, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Double, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Long, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Integer, Byref frmt As String)
Declare Sub print_matrix Overload(x() As Longint, Byref frmt As String)

#macro print_matrix_macro(x)
	#if __FB_VER_MINOR__ = 7
		Dim As Const FBC.FBARRAY Ptr pd = fb_ArrayGetDesc(x())
	#elseif __FB_VER_MINOR__ >= 8
		Dim As Const FBC.FBARRAY Ptr pd = FBC.ArrayDescriptorPtr(x())
	#endif
	If pd->dimensions=1 Then
		For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
			Print Using frmt;x(i);
		Next
		Print
	Elseif pd->dimensions=2 Then
		For i As Integer=pd->dimTb(0).lbound To pd->dimTb(0).ubound
			For j As Integer=pd->dimTb(1).lbound To pd->dimTb(1).ubound
				Print Using frmt;x(i,j);
			Next
			Print
		Next
	End If
#endmacro

Sub print_matrix(x() As Single, Byref frmt As String)
	print_matrix_macro(x)
End Sub
							  
Sub print_matrix(x() As Double, Byref frmt As String)
	print_matrix_macro(x)
End Sub

Sub print_matrix (x() As Long, Byref frmt As String)
	print_matrix_macro(x)
End Sub

Sub print_matrix (x() As Integer, Byref frmt As String)
	print_matrix_macro(x)
End Sub

Sub print_matrix (x() As Longint, Byref frmt As String)
	print_matrix_macro(x)
End Sub

Dim As Double x(1 To 6, 1 To 6) = {{ 1,  6, 15,  20,  15,   6}, _
							  {1,  7, 21,  35,  35,  21}, _
							  {1,  8, 28,  56,  70,  56}, _
							  {1,  9, 36,  84, 126, 126}, _
							  {1, 10, 45, 120, 210, 252}, _
							  {1, 11, 55, 165, 330, 462 }}
							  
Dim As Long y(0 To 7) = { 11, 22, 33, 44, 55, 99, -12, -13 }
Dim As Integer z(0 To 5) = { 1, 2, 3, 4, 5, 6 }
Dim As LongInt w(-3 To 3) = { 123, 234, 345, 456, 567, 678, 789 }
dim as double c(7,0) = {{1},{2},{3},{4},{5},{6},{7},{8}}

print_matrix(x(), "#####.###")
Print "-------------------"
print_matrix(y(), "#####.###")
Print "-------------------"
print_matrix(z(), "#####.###")
Print "-------------------"
print_matrix(w(), "#####.###")
Print "-------------------"
print_matrix(c(), "#####.###")
Post Reply