Translate

2015年12月18日 星期五

[Computer Science] Fast Integer Division and Modulus

Sometimes, fast integer division or modulus is required for high speed processing.
Division is normally the most uncomfortable operation for general processors.
Thanks to the math, it is able to convert divisions into bit shift operation by a trick.
Below is a proof of the trick, the idea is inspired from the glorious "Hacker's Delight" 


Since Modulus is a set of operations of division and multiplication like below

a%b = a - (a/b)*b

So, modulus can also take advantages from above trick

2014年9月15日 星期一

[VBA]Compact Access Database bypassing create a dummy database

https://github.com/walter426/VbaUtilities/blob/master/AccessObjUtilities.bas

Function to compact Access Database bypassing create a dummy database

'Comapact and renew Database
Public Function CompactAndRenewDb(Db_path As String) As String
    On Error GoTo Err_CompactAndRenewDb
    
    Dim FailedReason As String
    
    If FileExists(Db_path) = False Then
        FailedReason = Db_path
        GoTo Exit_CompactAndRenewDb
    End If

    
    Dim WShell As Object
    Dim FSO As Object
    
    Set WShell = CreateObject("WScript.Shell")
    Set FSO = CreateObject("Scripting.FileSystemObject")


    Dim Db_CP_path As String
    Db_CP_path = WShell.ExpandEnvironmentStrings("%TEMP%") & "\" & FSO.GetBaseName(Db_path) & "_Compacted" & "." & FSO.GetExtensionName(Db_path)

    
    If Len(Dir(Db_CP_path)) > 0 Then
        Kill Db_CP_path
    End If
    
    Call CompactDatabase(Db_path, Db_CP_path)
    
    
    If Len(Dir(Db_CP_path)) = 0 Then
        GoTo Exit_CompactAndRenewDb
    End If
    
    
    Kill Db_path
    Call CopyFileBypassErr(Db_CP_path, Db_path)
    Kill Db_CP_path
    
    
Exit_CompactAndRenewDb:
    CompactAndRenewDb = FailedReason
    Exit Function

Err_CompactAndRenewDb:
    FailedReason = Err.Description
    Resume Exit_CompactAndRenewDb
    
End Function

2014年8月20日 星期三

[VBA]Join two tables with any number of columns in a easy format

https://github.com/walter426/VbaUtilities/blob/master/SqlUtilities.bas
In order to join two tables into one, the SQL syntax is very complicated.
So I wrote below function to reduce the complexity.



''Create table which are joined from two tables
Public Function CreateTbl_JoinTwoTbl(Tbl_src_1_name As String, Tbl_src_2_name As String, JoinCond As String, ColSet_Join_1 As Variant, ColSet_Join_2 As Variant, Tbl_des_name As String, Optional ColSet_src_1 As Variant = Null, Optional ColSet_src_2 As Variant = Null, Optional ColSet_Order As Variant = Null) As String
    On Error GoTo Err_CreateTbl_JoinTwoTbl
    
    Dim FailedReason As String

    If TableExist(Tbl_src_1_name) = False Then
        FailedReason = Tbl_src_1_name & "does not exist!"
        GoTo Exit_CreateTbl_JoinTwoTbl
    End If
    
    If TableExist(Tbl_src_2_name) = False Then
        FailedReason = Tbl_src_2_name & "does not exist!"
        GoTo Exit_CreateTbl_JoinTwoTbl
    End If
    
    If IsNull(ColSet_Join_1) = True Then
        GoTo Exit_CreateTbl_JoinTwoTbl
    End If
    
    If IsNull(ColSet_Join_2) = True Then
        GoTo Exit_CreateTbl_JoinTwoTbl
    End If
    

    DelTable (Tbl_des_name)
    
    
    Dim Col_Idx As Integer
    
    
    With CurrentDb
        If IsNull(ColSet_src_1) = True Then
            Dim RS_Tbl_src As Recordset
            Set RS_Tbl_src = .OpenRecordset(Tbl_src_1_name)
            
            Dim fld_idx As Integer
            Dim fld_name As String
            
            ColSet_src_1 = Array()
            
            With RS_Tbl_src
                For fld_idx = 0 To .Fields.count - 1
                    fld_name = .Fields(fld_idx).name
                    Call AppendArray(ColSet_src_1, Array("[" & fld_name & "]"))
                Next fld_idx
                
                .Close
                
            End With 'RS_Tbl_src
        End If


        If IsNull(ColSet_src_2) = True Then
            Set RS_Tbl_src = .OpenRecordset(Tbl_src_2_name)
            
            With RS_Tbl_src
                Dim NumOfColSet_Join_found As Integer
                NumOfColSet_Join_found = 0
                
                ColSet_src_2 = Array()
                    
                For fld_idx = 0 To .Fields.count - 1
                    fld_name = .Fields(fld_idx).name

                    If NumOfColSet_Join_found <= UBound(ColSet_Join_2) And FindStrInArray(ColSet_Join_2, fld_name) > -1 Then
                        NumOfColSet_Join_found = NumOfColSet_Join_found + 1
                    Else
                        Call AppendArray(ColSet_src_2, Array("[" & fld_name & "]"))
                    End If
                Next fld_idx
    
                .Close
                
            End With 'RS_Tbl_src
        End If
    End With 'CurrentDb
    

    Dim SQL_Seg_Select As String
    SQL_Seg_Select = "SELECT " & "[" & Tbl_src_1_name & "]." & Join(ColSet_src_1, ", [" & Tbl_src_1_name & "].") & ", " & "[" & Tbl_src_2_name & "]." & Join(ColSet_src_2, ", [" & Tbl_src_2_name & "].")

    Dim SQL_Seg_JoinOn As String
    SQL_Seg_JoinOn = "("

    For Col_Idx = LBound(ColSet_Join_1) To UBound(ColSet_Join_1)
        SQL_Seg_JoinOn = SQL_Seg_JoinOn & "[" & Tbl_src_1_name & "].[" & ColSet_Join_1(Col_Idx) & "] = [" & Tbl_src_2_name & "].[" & ColSet_Join_2(Col_Idx) & "] AND "
    Next Col_Idx

    SQL_Seg_JoinOn = Left(SQL_Seg_JoinOn, Len(SQL_Seg_JoinOn) - 4) & ")"

    
    Dim SQL_Seg_OrderBy As String
    SQL_Seg_OrderBy = ""
    
    If IsNull(ColSet_Order) = False Then
        SQL_Seg_OrderBy = "ORDER BY "
        
        For Col_Idx = LBound(ColSet_Order) To UBound(ColSet_Order)
            SQL_Seg_OrderBy = SQL_Seg_OrderBy & "[" & Tbl_src_1_name & "].[" & ColSet_Order(Col_Idx) & "], "
        Next Col_Idx
        
        SQL_Seg_OrderBy = Left(SQL_Seg_OrderBy, Len(SQL_Seg_OrderBy) - 2)
        
    End If
    
    
    Dim SQL_cmd As String
    
    SQL_cmd = SQL_Seg_Select & " " & vbCrLf & _
                "INTO [" & Tbl_des_name & "] " & vbCrLf & _
                "FROM [" & Tbl_src_1_name & "] " & JoinCond & " JOIN [" & Tbl_src_2_name & "] " & vbCrLf & _
                "ON " & SQL_Seg_JoinOn & vbCrLf & _
                SQL_Seg_OrderBy & " " & vbCrLf & _
                ";"

    RunSQL_CmdWithoutWarning (SQL_cmd)


Exit_CreateTbl_JoinTwoTbl:
    CreateTbl_JoinTwoTbl = FailedReason
    Exit Function

Err_CreateTbl_JoinTwoTbl:
    FailedReason = Err.Description
    Resume Exit_CreateTbl_JoinTwoTbl
    
End Function

2014年8月6日 星期三

[VBA] Transfer multiple objects(e.g. tables, queries) in a database

https://github.com/walter426/VbaUtilities

'Transfer multiple objects in a database
Public Function TransferObjSetInDb(TransferType As Variant, DatabaseType As String, DatabaseName As String, ObjectType As Variant, SrcList As Variant, DesList As Variant, Optional StructureOnly As Boolean = False, Optional StoreLogin As Boolean = False) As String
    On Error GoTo Err_TransferObjSetInDb

    Dim FailedReason As String

    If Len(Dir(DatabaseName)) = 0 Then
        FailedReason = DatabaseName
        GoTo Exit_TransferObjSetInDb
    End If
 

    If VarType(DesList) <> vbArray + vbVariant Then
        DesList = SrcList
    End If
 

    If UBound(SrcList) <> UBound(DesList) Then
        FailedReason = "No. of elements in SrcList and DesList are not equal"
        GoTo Exit_TransferObjSetInDb
    End If
 
 
    Dim TblNameIdx As Integer

    For TblNameIdx = 0 To UBound(SrcList)
        If ObjectType = acTable Then
            DelTable (DesList(TblNameIdx))
        ElseIf ObjectType = acQuery Then
            DelQuery (DesList(TblNameIdx))
        End If


        On Error Resume Next
        DoCmd.TransferDatabase TransferType, DatabaseType, DatabaseName, ObjectType, SrcList(TblNameIdx), DesList(TblNameIdx), StructureOnly, StoreLogin
        On Error GoTo Next_TblNameIdx
     
Next_TblNameIdx:
    Next TblNameIdx
 
    On Error GoTo Err_TransferObjSetInDb
 
 
Exit_TransferObjSetInDb:
    TransferObjSetInDb = FailedReason
    Exit Function

Err_TransferObjSetInDb:
    FailedReason = Err.Description
    Resume Exit_TransferObjSetInDb
 
End Function

2014年7月18日 星期五

[Python] Coordinate Transform between Grid and Geographic (e.g HK1980 and WGS84)

https://pypi.python.org/pypi/CoorTransform_GirdGeographic
https://github.com/walter426/Python_CoorTransform_GirdGeographic

It is a python package refer to my previous VBA module, http://waltertech426.blogspot.com/2014/07/vba-coordinate-transform-from-hk1980-to.html.

This python package is to provide functions for coordinate transform from grid to geographic, or vice versa.
Only HK1980 and WGS84 are set as the default conversion.

#Coordinate Transformation according to http://www.geodetic.gov.hk/data/pdf/explanatorynotes_c.pdf

from math import * 


class CoorTransformer_GridAndGeographic:
#HK80 Datum is set as default
def __init__(self, E0 = 836694.05, N0 = 819069.8, Lng0 = 114.178556, Lat0 = 22.312133, m_0 = 1, M0 = 2468395.723, a = 6378388, e2 = 6.722670022 * pow(10,-3)):
#Initilalize Projection Parameter
self.E0 = E0
self.N0 = N0
self.Lng0 = Lng0 * pi / 180
self.Lat0 = Lat0 * pi / 180
self.m_0 = m_0
self.M0 = M0
self.a = a
self.e2 = e2
e4 = pow(e2, 2)


#Meridian distance Coefficients
self.A0 = 1 - (e2 / 4) - (3 * e4) / 64
self.A2 = (3.0 / 8.0) * (e2 + (e4 / 4.0))
self.A4 = (15.0 / 256.0) * e4


def MeridianDist(self, Lat):
return self.a * (self.A0 * Lat - self.A2 * sin(2 * Lat) + self.A4 * sin(4 * Lat))


#Coordinate Transform from grid to geographic in degree
def CoorTransform_GridToGeographic(self, Easting, Northing, accuracy = 9):
E0 = self.E0
N0 = self.N0
Lng0 = self.Lng0
Lat0 = self.Lat0
m_0 = self.m_0
M0 = self.M0
a = self.a
e2 = self.e2

#Convert from grid to geographic
#Calculate Lat_p by iteration of Meridian distance,
E_Delta = Easting - E0
N_delta = Northing - N0
Mp = (N_delta + M0) / m_0

Lat_min = -90 * pi / 180
Lat_max = 90 * pi / 180

accuracy = pow(10, -accuracy)

  #Newton 's method A0 = self.A0 A2 = self.A2 A4 = self.A4 Lat_p = (Lat_max + Lat_min) / 2 f = 1.1 while abs(f) > accuracy: f = Mp - self.MeridianDist(Lat_p) f_d1 = -a * (A0 - A2 * 2 * cos(2 * Lat_p) + A4 * 4 * cos(4 * Lat_p)) Lat_p = Lat_p - (f / f_d1)



t_p = tan(Lat_p)
v_p = a / pow((1.0 - e2 * pow(sin(Lat_p), 2)), (1 / 2))
p_p = (a * (1.0 - e2)) / pow((1 - e2 * pow(sin(Lat_p), 2)), (3 / 2))
W_p = v_p / p_p

Lng = Lng0 + (1 / cos(Lat_p)) * ((E_Delta / (m_0 * v_p)) - (1 / 6) * pow((E_Delta / (m_0 * v_p)), 3) * (W_p + 2 * pow(t_p, 2)))
Lat = Lat_p - (t_p / ((m_0 * p_p))) * (pow(E_Delta, 2) / ((2 * m_0 * v_p)))


return [Lng / pi * 180, Lat / pi * 180]


#Coordinate Transform from geographic in degree to grid
def CoorTransform_GeographicToGrid(self, Lng, Lat):
E0 = self.E0
N0 = self.N0
Lng0 = self.Lng0
Lat0 = self.Lat0
m_0 = self.m_0
M0 = self.M0
a = self.a
e2 = self.e2

#Convert Lat and Lng from degree to radian
Lng = Lng * pi / 180
Lat = Lat * pi / 180


#Convert from geographic to grid
Lng_Delta = Lng - Lng0
M = self.MeridianDist(Lat)

t_s = tan(Lat)
v_s = a / pow((1.0 - e2 * pow(sin(Lat), 2)), (1 / 2))
p_s = (a * (1.0 - e2)) / pow((1 - e2 * pow(sin(Lat), 2)), (3 / 2))
W_s = v_s / p_s

Easting = E0 + m_0 * v_s * (Lng_Delta * cos(Lat) + (1 / 6) * pow(Lng_Delta, 3) * pow(cos(Lat), 3) * pow(W_s - t_s, 2))
Northing = N0 + m_0 * ((M - M0) + v_s * (pow(Lng_Delta, 2) / 4) * sin(2 * Lat))


return [Easting, Northing]


#Coordinate Transform from HK1980 grid to WGS84 geographic in degree
def CoorTransform_Hk1980ToWgs84(self, Easting, Northing, Delimiter = ""):
LngLat_HK1980 = self.CoorTransform_GridToGeographic(Easting, Northing)

Lng_WGS84 = LngLat_HK1980[0] + (8.8 / 3600)
Lat_WGS84 = LngLat_HK1980[1] - (5.5 / 3600)


if Delimiter == "":
return [Lng_WGS84, Lat_WGS84]
else:
return str(Lng_WGS84) + Delimiter + str(Lat_WGS84)


#Coordinate Transform from WGS84 geographic in degree to HK1980 grid
def CoorTransform_Wgs84ToHK1980(self, Lng, Lat, Delimiter = ""):
Lng_HK1980 = Lng - (8.8 / 3600)
Lat_HK1980 = Lat + (5.5 / 3600)

EastNorth_HK1980 = self.CoorTransform_GeographicToGrid(Lng_HK1980, Lat_HK1980)


if Delimiter == "":
return EastNorth_HK1980
else:
return  str(EastNorth_HK1980(0)) & Delimiter & str(EastNorth_HK1980(1))

[VBA] Coordinate Transform between Grid and Geographic (e.g HK1980 and WGS84)

https://github.com/walter426/VbaUtilities/blob/master/GeometryUtilities.bas

Below is the function set to perform the coordinate transform between Grid and Geographic, the pair of HK1980 and WGS84 is taken as the example for this kind of transform

'Coordinate Transformation according to http://www.geodetic.gov.hk/data/pdf/explanatorynotes_c.pdf

Public Const Pi = 3.14159265359

'Coordinate Transform from HK1980 grid to WGS84 geographic in degree
Public Function CoorTransform_Hk1980ToWgs84(Easting, Northing, Optional Delimiter As String = "") As Variant
    'Initilalize Constant
    E0 = 836694.05
    N0 = 819069.8
    Lng0 = 114.178556
    Lat0 = 22.312133
    m_0 = 1
    M0 = 2468395.723
    a = 6378388
    e2 = 6.722670022 * (10 ^ (-3))
    
    LngLat_HK1980 = CoorTransform_GridToGeographic(E0, N0, Lng0, Lat0, m_0, M0, a, e2, Easting, Northing)

    Lng_WGS84 = LngLat_HK1980(0) + (8.8 / 3600)
    Lat_WGS84 = LngLat_HK1980(1) - (5.5 / 3600)
    
    
    If Delimiter = "" Then
        CoorTransform_Hk1980ToWgs84 = Array(Lng_WGS84, Lat_WGS84)
    Else
        CoorTransform_Hk1980ToWgs84 = Lng_WGS84 & Delimiter & Lat_WGS84
    End If
    
    
End Function



Public Function CoorTransform_GridToGeographic(E0, N0, Lng0, Lat0, m_0, M0, a, e2, Easting, Northing, Optional accuracy = 6) As Variant
    'Meridian distance Coefficients
    A0 = 1 - (e2 / 4) - (3 * (e2 ^ 2) / 64)
    A2 = (3 / 8) * (e2 + ((e2 ^ 2) / 4))
    A4 = (15 / 256) * (e2 ^ 2)
    

    'Convert the Lat0 and Lng0 from degree to radian
    Lng0 = Lng0 * Pi / 180
    Lat0 = Lat0 * Pi / 180
    
    
    'Convert from grid to geographic
    'Calculate Lat_p by iteration of Meridian distance,
    E_Delta = Easting - E0
    N_delta = Northing - N0
    Mp = (N_delta + M0) / m_0
    
    Lat_min = -90 * Pi / 180
    Lat_max = 90 * Pi / 180

    accuracy = 10 ^ (-accuracy)
    

     'Newton 's method
    Lat_p = (Lat_max + Lat_min) / 2
    f = 1.1
    
    Do While Abs(f) > accuracy
        f = Mp - a * (A0 * Lat_p - A2 * Sin(2 * Lat_p) + A4 * Sin(4 * Lat_p))
        f_d1 = -a * (A0 - A2 * 2 * Cos(2 * Lat_p) + A4 * 4 * Cos(4 * Lat_p))
        Lat_p = Lat_p - (f / f_d1)

    Loop
    
    
    t_p = Tan(Lat_p)
    v_p = a / ((1 - e2 * Sin(Lat_p) ^ 2) ^ (1 / 2))
    p_p = (a * (1 - e2)) / ((1 - e2 * Sin(Lat_p) ^ 2) ^ (3 / 2))
    W_p = v_p / p_p
    

    Lng = Lng0 + (1 / Cos(Lat_p)) * ((E_Delta / (m_0 * v_p)) - (1 / 6) * ((E_Delta / (m_0 * v_p)) ^ 3) * (W_p + 2 * (t_p ^ 2)))
    Lat = Lat_p - (t_p / ((m_0 * p_p))) * ((E_Delta ^ 2) / ((2 * m_0 * v_p)))


    CoorTransform_GridToGeographic = Array(Lng / Pi * 180, Lat / Pi * 180)
    
    
End Function



Public Function CoorTransform_Wgs84ToHK1980(Lng, Lat, Optional Delimiter As String = "") As Variant
    'Initilalize Constant
    E0 = 836694.05
    N0 = 819069.8
    Lng0 = 114.178556
    Lat0 = 22.312133
    m_0 = 1
    M0 = 2468395.723
    a = 6378388
    e2 = 6.722670022 * (10 ^ (-3))
    
    Lng_HK1980 = Lng - (8.8 / 3600)
    Lat_HK1980 = Lat + (5.5 / 3600)
    
    EastNorth_HK1980 = CoorTransform_GeographicToGrid(E0, N0, Lng0, Lat0, m_0, M0, a, e2, Lng_HK1980, Lat_HK1980)
    
    
    If Delimiter = "" Then
        CoorTransform_Wgs84ToHK1980 = EastNorth_HK1980
    Else
        CoorTransform_Wgs84ToHK1980 = EastNorth_HK1980(0) & Delimiter & EastNorth_HK1980(1)
    End If
    
    
End Function


'Coordinate Transform from geographic in degree to grid
Public Function CoorTransform_GeographicToGrid(E0, N0, Lng0, Lat0, m_0, M0, a, e2, Lng, Lat) As Variant
    'Meridian distance Coefficients
    A0 = 1 - (e2 / 4) - (3 * (e2 ^ 2) / 64)
    A2 = (3 / 8) * (e2 + ((e2 ^ 2) / 4))
    A4 = (15 / 256) * (e2 ^ 2)
    

    'Convert Lat and Lng from degree to radian
    Lng0 = Lng0 * Pi / 180
    Lat0 = Lat0 * Pi / 180
    
    Lng = Lng * Pi / 180
    Lat = Lat * Pi / 180
    
    
    'Convert from geographic to grid
    Lng_Delta = Lng - Lng0
    M = a * (A0 * Lat - A2 * Sin(2 * Lat) + A4 * Sin(4 * Lat))

    t_s = Tan(Lat)
    v_s = a / ((1 - e2 * Sin(Lat) ^ 2) ^ (1 / 2))
    p_s = (a * (1 - e2)) / ((1 - e2 * Sin(Lat) ^ 2) ^ (3 / 2))
    W_s = v_s / p_s
    

    Easting = E0 + m_0 * v_s * (Lng_Delta * Cos(Lat) + (1 / 6) * (Lng_Delta ^ 3) * (Cos(Lat) ^ 3) * (W_s - t_s ^ 2))
    Northing = N0 + m_0 * ((M - M0) + v_s * ((Lng_Delta ^ 2) / 4) * Sin(2 * Lat))


    CoorTransform_GeographicToGrid = Array(Easting, Northing)
    
    
End Function

2014年6月3日 星期二

[VBA] Append Access SQL Object(Table, Query) to Excel worksheet

Code:

'Append Access SQL Object(Table, Query) to Excel worksheet, and activate it 
Public Function AppendSqlObjToAndActivateWs(oWs As Worksheet, SqlObj_name As String, Optional AddBorder As Boolean = False) As String
    On Error GoTo Err_AppendSqlObjToAndActivateWs
    
    Dim FailedReason As String

    If TableExist(SqlObj_name) = False And QueryExist(SqlObj_name) Then
        FailedReason = SqlObj_name & " does not exist!"
        GoTo Exit_AppendSqlObjToAndActivateWs
    End If
    
    With oWs
        'Have to activate the worksheet for copying query with no error!
        .Activate
        
        'Store the new start row
        Dim RowEnd_old As Long
        Dim RowStart_new As Long
        
        RowEnd_old = .UsedRange.Rows.count
        RowStart_new = RowEnd_old + 1
        
        'Append SqlObj_name to the sheet
        
        'Create Recordset object
        Dim rs As DAO.Recordset
        Set rs = CurrentDb.OpenRecordset(SqlObj_name, dbOpenSnapshot)

        .Range("A" & CStr(.UsedRange.Rows.count + 1)).CopyFromRecordset rs, 65534


        'Copy format from previous rows to new rows
        .Range(.Cells(RowEnd_old, 1), .Cells(RowEnd_old, .UsedRange.Columns.count)).Copy
        .Range(.Cells(RowStart_new, 1), .Cells(.UsedRange.Rows.count, .UsedRange.Columns.count)).PasteSpecial Paste:=xlPasteFormats
        

        If AddBorder = True Then
            'Add border at the last row
            With .UsedRange.Rows(.UsedRange.Rows.count).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With '.UsedRange.Rows(.UsedRange.Rows.count).Borders(xlEdgeBottom)
        End If
        
        
    End With 'oWs
    
    
Exit_AppendSqlObjToAndActivateWs:
    AppendSqlObjToAndActivateWs = FailedReason
    Exit Function

Err_AppendSqlObjToAndActivateWs:
    FailedReason = Err.Description
    Resume Exit_AppendSqlObjToAndActivateWs
    
End Function