确定函数调用的方法

Determine what method is called by a function

我正在研究 bayesnet toolbox 中的代码,我正在努力了解如何判断特定函数调用分派了哪个方法。

例如调用

convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence)

对于特定类型的 CPD,有多个 convert_to_pot 函数 (in folders here),我认为,所谓的函数是由对象 属性 决定的 bnet.CPD。如果 CPD 节点是离散的,我认为它调用 @discrete_CPD,但有没有办法确定?或者你能从函数调用产生的结构中分辨出来吗?谢谢

methods(convert_to_pot) returns 未定义.


一个有效的例子

    % set up graph and CPD's
    N = 4; 
    dag = zeros(N,N); 
    C = 1; S = 2; R = 3; W = 4;
    dag(C,[R S]) = 1; 
    dag(R,W) = 1;
    dag(S,W)=1;
    discrete_nodes = 1:N;
    node_sizes = 2*ones(1,N);
    bnet = mk_bnet(dag, node_sizes, 'discrete', discrete_nodes);
    bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
    bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
    bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
    bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);

    %evidence node
    evidence = cell(1,N);
    evidence{W} = 2;    
    ns = bnet.node_sizes(:); [2 2 2 2]
    onodes = find(~isemptycell(evidence)); % 4
    hnodes = find(isemptycell(evidence)); % 1 2 3
    pot_type = determine_pot_type(bnet, onodes); % 'd'  :discrete
    fam = family(bnet.dag, 4); % 2 3 4

函数调用和结果

    pot = convert_to_pot(bnet.CPD{4}, pot_type, fam(:), evidence)
%     discrete potential object
%     domain: [2 3 4]
%          T: [2x2 double]
%      sizes: [2 2 1]

根据评论更新;

>> methods(bnet.CPD{e})

Methods for class tabular_CPD:

CPD_to_CPT              learn_params            maximize_params         update_ess              
bayes_update_params     log_marg_prob_node      reset_ess               update_ess_simple       
display                 log_nextcase_prob_node  set_fields              
get_field               log_prior               tabular_CPD             

>> class(bnet.CPD{e})

ans =

tabular_CPD

>> superclasses(bnet.CPD{4})

No class tabular_CPD.

但是@tabular_CPD没有convert_to_pot函数。

我无法从问题中分辨出来(而且我没有安装此工具箱),但根据您的描述,听起来 bnet.CPD{e} 是一个 class 并且convert_to_pot 是一种 class 方法,适用于各种类型的 classes。在那种情况下,也许你可以试试 methods(bnet.CPD{e}) 代替?