9.21.2019

最近看的影集及電影(雷)

這幾天,因為網友推薦,看了關於我轉生變成史萊姆這檔事
看完第一季,覺得還不錯,只是推薦我看的網友,知道史萊姆是什麼,
但是居然沒有聽過勇者鬥惡龍這個遊戲。
我和網友都懷疑,主角會不會變成最後大魔王,要跟勇者對決?

今天,為了消秏MOD的看片金,看了 詐騙女神 這部片,
覺得還OK, 好在最後的走向我有喜歡,如果變成純愛劇,
我會覺得很爛…

7.05.2019

0/0未定義的原因 (小學數學)

0/0是未定義,那為何0/0不是1呢?是否有反例?
我想到的反例如下:(這是我自己想的,如有錯誤,還請指正)
Assume 0/0 = 1,
if 2x = 3x
=> x = 1.5x, so x = 0
=> 0 = 1.5 * 0
兩邊同除0
=> 0/0 =1.5 * 0/0
=> (0/0) = 1.5 * (0/0)
We assume 0/0 = 1
=> 1 = 1.5 * 1
=> 1 = 1.5, 所以這是0/0 = 1的反例

那麼0/0是否等於0呢?
這個我不知道真正的反例,
但是這樣0/0就不能上下消去得到1了。
至於0/0等於其它數值,可以簡單得到反例,
這就不提了。



6.13.2019

Differential and Partial differential


Differential of a function

dy=f'(x)\,dx,
where  is the derivative of f with respect to x, and dx is an additional real variable (so that dy is a function of x and dx). The notation is such that the equation
holds, where the derivative is represented in the Leibniz notation dy/dx, and this is consistent with regarding the derivative as the quotient of the differentials. One also writes


satisfies
where the error ε in the approximation satisfies ε/Δx → 0 as Δx → 0. In other words, one has the approximate identity
in which the error can be made as small as desired relative to Δx by constraining Δx to be sufficiently small; that is to say,
as Δx → 0. For this reason, the differential of a function is known as the principal (linear) part in the increment of a function: the differential is a linear function of the increment Δx, and although the error ε may be nonlinear, it tends to zero rapidly as Δx tends to zero.

the partial differential of y with respect to any one of the variables x1 is the principal part of the change in y resulting from a change dx1 in that one variable. The partial differential is therefore

參:
https://en.wikipedia.org/wiki/Differential_of_a_function


全微分和偏微分有什麼不同
假設 z = x^7 + y^3 + 3(x^2)*(y^4) + 4xy + 98 
z為應變數 , x y為自變數 

全微分 : 
就是所有變數通通都要微分 
dz = 7x^6dx + 3y^2dy + 6x(y^4)dx + 12(x^2)*(y^3)dy + 4ydx + 4xdy + 0 
ps. 兩變數相乘的 , 如同一般乘法的微分 , 前微後不微+前不微後微 

偏微分 : 
只對要微的自變數微分 , 其他的都當作常數 
1.對 x 偏微 
∂z/∂x = 7x^6 + 0 + 6x(y^4) + 4y + 0 

2. 對 y 偏微 
∂z/∂y = 0 + 3y^2 + 12(x^2)*(y^3) + 4x + 0

參:
https://tw.answers.yahoo.com/question/index?qid=20050930000013KK04937


5.22.2019

pdf2txt

pdf2txt.py

pdf2txt.py从PDF文件中提取文本内容。它提取所有要以编程方式呈现的文本,即以ASCII或Unicode字符串表示的文本。它无法识别绘制为需要光学字符识别的图像的文本。它还为每个文本部分提取相应的位置,字体名称,字体大小,书写方向(水平或垂直)。访问受到限制时,您需要为受保护的PDF文档提供密码。您无法从没有提取权限的PD文档中提取任何文本。
$ pdf2txt.py -o output.html samples/naacl06-shinyama.pdf
(extract text as an HTML file whose filename is output.html)

$ pdf2txt.py -V -c euc-jp -o output.html samples/jo.pdf
(extract a Japanese HTML file in vertical writing, CMap is required)

$ pdf2txt.py -P mypassword -o output.txt secret.pdf
(extract a text from an encrypted PDF file)
有关详细信息,请参阅/docs/index.html。

PDFMiner.six

PDFMiner.six is a fork of PDFMiner using six for Python 2+3 compatibility
Build Status PyPI version
PDFMiner is a tool for extracting information from PDF documents. Unlike other PDF-related tools, it focuses entirely on getting and analyzing text data. PDFMiner allows one to obtain the exact location of text in a page, as well as other information such as fonts or lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purposes than text analysis.


from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.layout import LTTextBoxHorizontal
import sys, os

os.environ["PYTHONIOENCODING"] = 'utf-8'
sys.stdout.reconfigure(encoding='utf-8')
#print(sys.stdout.encoding)
#print(os.environ["PYTHONIOENCODING"])
            
document = open('Pfizer1.pdf', 'rb')
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.get_pages(document):
    interpreter.process_page(page)
    layout = device.get_result()
    for element in layout:
        if isinstance(element, LTTextBoxHorizontal):
            obj = element._objs[0]
            print("x_cor: %.2f " % obj.bbox[0])
            print("y_cor: %.2f" % obj.bbox[1])
            print("length: %.2f" % obj.bbox[2])
            print("height: %.2f" % obj.bbox[3])
            text = obj.get_text().replace('\n','')
            #btext = text.encode(encoding='utf-8')
            print("text: ", text)
            print("--------------------")

8.27.2018

Python 3 call C by ctypes

point.h
/* Point.h */
/* Simple structure for ctypes example */
typedef struct {
    int x;
    int y;
} Point;

point.c
/* Point.c */
#include "stdio.h"
#include "point.h"

/* Display a Point value */
void show_point(Point point) {
    printf("Point in C      is (%d, %d)\n", point.x, point.y);
}

/* Increment a Point which was passed by value */
void move_point(Point point) {
    show_point(point);
    point.x++;
    point.y++;
    show_point(point);
}

/* Increment a Point which was passed by reference */
void move_point_by_ref(Point *point) {
    show_point(*point);
    point->x++;
    point->y++;
    show_point(*point);
}

/* Return by value */
Point get_point(void) {
    static int counter = 0;
    Point point = { counter++, counter++ };
    printf("Returning Point    (%d, %d)\n", point.x, point.y);
    return point;
}


For mingw32:
gcc -fPIC -shared -msse4.2 point.c -o point.dll

使用point.py連point.dll:
import ctypes
def wrap_function(lib, funcname, restype, argtypes):
    """Simplify wrapping ctypes functions"""
    func = lib.__getattr__(funcname)
    func.restype = restype
    func.argtypes = argtypes
    return func
   
class Point(ctypes.Structure):
    _fields_ = [('x', ctypes.c_int), ('y', ctypes.c_int)]
    def __repr__(self):
        return '({0}, {1})'.format(self.x, self.y)
       
libc = ctypes.CDLL('point.dll')
show_point = wrap_function(libc, 'show_point', None, [Point])
p = Point(1, 2)
show_point(p)

正常的話,會出現:
Point in C      is (1, 2)

如果出現以下錯誤訊息:
OSError: [WinError 193] %1 is not a valid Win32 application

可能是因為Python是x64版的,但是dll是32 bits
詳參:

Error loading DLL in python, not a valid win32 application


ctypes更複雜的例子,可參考:

8.25.2018

"undefined reference to" 問題解決方法

在此,只舉個靜態庫的例子,假設源碼如下。
先把test.c編譯成靜態庫(.a)文件
  1. gcc -c test.c
  2. ar -rc test.a test.o
至此,我們得到了test.a文件。我們開始編譯main.c
  1. gcc -c main.c
這時,則生成了main.o文件,然後我們再通過如下命令進行連結希望得到可執行程序。
你會發現,編譯器報錯了:
  1. /tmp/ccCPA13l.o: In function `main':
  2. main.c:(.text+0x7): undefined reference to `test'
  3. collect2: ld returned 1 exit status
其根本原因也是找不到test函數的實現文件,由於該test函數的實現在test.a這個靜態庫中的,故在連結的時候需要在其後加入test.a這個庫,連結命令修改為如下形式即可。
  1. gcc -o main main.o ./test.a //註:./ 是給出了test.a的路徑
【擴展】:同樣,為了把問題說清楚,上面我們把代碼的編譯連結分開了,如果希望一次性生成可執行程序,則可以對main.c和test.a執行如下命令。
  1. gcc -o main main.c ./test.a //同樣,如果不加test.a也會報錯


原文網址:https://read01.com/GAQBKx.html

.o, .a, .so, .la 如何產生

o, .a, .so, .la 如何產生

  • 建立 .o
    $ gcc -c test.c
  • 建立 .a
    $ ar -r libtest.a test1.o test2.o
  • 建立動態函式庫 .so
    $ gcc -Wall -fpic -shared test1.c test2.c -o libtest.so
  • 鍊結函式庫
    $ gcc -Wall -fpic -shared -Ltest test3.c -o libtest.so
  • 建立 .la, .la 一般會使用 Makefile 產生, 可以透過下述命令產生:
    $ libtool --mode=link gcc -o libmylib.la -rpath /usr/lib –L/usr/lib –la

Linux 的 .a / .so / .la 函式庫的差異

  • .o (obj file) 是目標物件函式庫, 等同於 Windows 的 .obj
  • .a 為靜態函式庫, 可以是 一個 或 多個 .o 合在一起, 用於靜態連結
  • .la 為 libtool 生成的共享函式庫, 其實是個設定的檔案. 可以用 file 代替
  • .la 可用 vi 來查看
  • .so 為動態函式庫, 類似 Windows 的 DLL(Dynamic-link library)檔.
  • 補充: 還有一種附檔名為 .ko 的檔案, 不過它是 Linux 核心使用的動態連結文件, 屬於模組程式, 用來在 Linux 系統啟動時, 加掛核心模組用.