BICG算法

伪代码流程:
在这里插入图片描述
知道此时的残差r,前面k次的搜索方向P,现在需要求的是第k次的搜索方向Pk;
有了 Xk\ X_k,搜索方向Pk,下一步就改确定搜索步长了,求它的思想是使取得极值,即导数为0。一旦求出了,则下一个迭代点也就求出了。表达式对求导为0后可求得αk\alpha_k

   “””
    bicg算法求解
    Input variables:
    b -- the problem to be solved is A * x = b.

    Keyword variables:
    x -- initial guess of x, default value is 0.
    r -- 残差矢量
    p -- 搜索方向序列
    alpha -- 步长
    conj(r) -- 复数的共轭
    dot -- 点积
    
    rho_step  --rho_step(alpha, p, r, v, x) updates r and x, and returns 
    	rho and the error.  Specifically, rho_step performs:
            x = x + alpha * p
            r = r - alpha * v
            rho_(k+1) = (r dot r)
            err = (conj(r) dot r)
    alpha_step -- alpha_step(rho_k, rho_(k-1), p, r, v) updates p and v, and 
        returns alpha. Specifically, alpha_step performs:
            p = r + (rho_k / rho_(k-1)) * p
            v = A * p
            alpha = rho_k / (p dot v)
            
    zeros -- zeros() creates a zero-initialized vector. 
    err_thresh -- the relative error threshold, default 1e-6.
    max_iters -- maximum number of iterations allowed, default 1000.
    reporter -- function to report progress.

    Output variables:
    x -- the approximate answer of A * x = b.
    err -- a numpy array with the error value at every iteration.
    success -- True if convergence was successful, False otherwise.
  “”“

示意图
optimal step size (in green) and conjugate vector (in red),
在这里插入图片描述

C++ 模板引擎:
ctemplate大体上分为两个部分,一部分是模板,另一部分是数据字典。模板定义了界面展现的形式(V),数据字典就是填充模板的数据(M),你自己写业务逻辑去控制界面展现(C),典型的MVC模型。

  • 变量,{{变量名}},用两个大括号包含的就是变量名,在c++代码中,可以对变量赋值,任何类型的值都可以(如字符,整数,日期等)。
    Ctemplate 的介绍
    源代码

C调用Python的类及方法

->第一步是导入.py文件:

使用PyObject* pModule来存储导入的.py文件模块, 调用的方法是PyImport_ImportModule(path):  PyObject* pModule = PyImport_ImportModule("testpy"); 
使用PyObject* pDict来存储导入模块中的方法字典, 调用的方法是PyModule_GetDict(module):  PyObject* pDict = PyModule_GetDict(pModule); 
这样就完成了.py文件的导入.

->第二步是导入已导入模块中的方法或类:

获取方法, 调用的方法是PyDict_GetItemString(dict, methodName): PyObject* pFunHi = PyDict_GetItemString(pDict, "sayhi"); 
获取类, 调用的方法同上, 注意红体部分的字符串对应于.py文件中的类/方法名:  PyObject* pClassSecond = PyDict_GetItemString(pDict,"Second"); 

->第三步是使用导入的方法或类:

使用方法, 调用PyObject_CallFunction(pFunc, "s", args)即可:  PyObject_CallFunction(pFunHi, "s", "lhb"); 
使用类构造对象, 调用PyInstance_New(pClass, NULL, NULL)即可:  PyObject* pInstanceSecond = PyInstance_New(pClassSecond, NULL, NULL); , 注意其中的pClassSecond为第二步.2中获取的类指针
使用类对象的方法, 调用PyObject_CallMethod(pInstance, methodname, "O", args)即可:  PyObject_CallMethod(pInstanceSecond,"invoke", "O", pInstancePerson); 

步骤
代码demo


参考资料:

  1. https://en.wikipedia.org/wiki/Biconjugate_gradient_method
  2. https://www.cnblogs.com/tornadomeet/p/3265225.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章