关于检查C++中的集合是否是代数组的几个问题

Don*_*uth 8 c++ algorithm math linear-algebra equation-solving

我开始创建一个用抽象代数做事的库.现在我正在尝试创建一个检查集合是否为组的函数.它应该是不言自明的:

在数学中,一个群体是一组元素,同时结合了任何两个元素的运算,形成满足四个条件的第三个元素,称为群组公理,即闭包,关联性,同一性和可逆性.组中最熟悉的一个例子是整数集和加法运算; 任何两个整数的加法形成另一个整数.(http://en.wikipedia.org/wiki/Group_(mathematics))

#include <set>
#include <iostream>

template <typename ObType, typename BinaryFunction>
bool isGroup(const std::set<ObType> & S, BinaryFunction & op)
{
    /*
       isGroup returns true or false depending on whether the set S
       along with the operator op is a group in the Algebraic sense.
       That is, S is a group if and only if all the 4 following
       conditions are true: 
            (1) If a, b in S, then a op b in S
            (2) If a, b, c in S, then (a + b) + c = a + (b + c)
            (3) There is an element 0 in S such that a + 0 = 0 + a for all a in S
            (4) If a in S, then there is a b in S such that a + b = b + a = 0
    */
    typename std::set<ObType>::const_iterator beg(S.cbegin()), offend(S.cend());
    bool noProblems = true;
    for (std::set<ObType>::const_iterator ia = beg; ia != offend && noProblems; ++ia)
    {
        for (std::set<ObType>::const_iterator ia = beg; ib != offend && noProblems; ++ib)
        {
            // ---------- (1) --------------
            if (S.count(op(*ia, *ib)) == 0) 
                noProblems = false;
            // -----------------------------
            for (std::set<ObType>::const_iterator ic = beg; ic != offend && noProblems; ++ic)
            {
                // ---------- (2) -------------
                if (((*ia + *ib) + *ic) != (*ia + (*ib + *ic)))
                    noProblems = false;
                // ----------------------------
            }
        }
    }

    return noProblems;
}

template <typename T>
class Plus
{
    public:
        T operator() (const T & x, const T & y) { return x + y; };  
};

int main()
{
    std::set<int> S1 = { 0, 1, -1 };
    std::set<int> S2 = { 0 };
    class Plus<int> p;
    std::cout << isGroup(S1, p);
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

没有编译器错误,但我有几个问题:

  • 我如何检查(3)(4)我的循环窝里面?一世
  • 后来,我想检查整个套像本地对象是否intlong是组.如何设置S1等于std::set所有longs?

4pi*_*ie0 2

您应该创建一个类来通过操作op( + ) 来表达集合的概念(注意:这个“ + ”不是普通的算术 + 所以

// ---------- (2) -------------
if (((*ia + *ib) + *ic) != (*ia + (*ib + *ic)))
    noProblems = false;
Run Code Online (Sandbox Code Playgroud)

错了,应该是这个

// ---------- (2) -------------
if ( op( op(*ia,*ib), *ic) != op( *ia, op( *ib, *ic)))
    noProblems = false;
Run Code Online (Sandbox Code Playgroud)

)、该集合(一个容器)的值(或者更确切地说是元素),以及一个称为1(或e )元素的特殊元素(对于具有(称为操作)加法+的整数R为 0 ,但对于具有(称为操作)加法 + 的整数R\0为 1乘法“x”)。您需要在您的班级中添加1 。检查(3)和(4)是绝对必要的。此外,恒等式1通常不是整数 0,而是对某些特殊恒等元素的描述,如果x 本身与1、( e )进行+运算,并且1 + x = x,则该恒等元素将产生相同的元素 x 。(如果运算“+”是可交换的,则可以跳过一个表达式,如果 S 是阿贝尔群,则为真)。

现在你要做什么取决于你是否想引入提示参数。要在给定集合中查找带有提示的单位元素,您可以编写

template <typename ObType, typename BinaryFunction>
bool isGroup( const std::set<ObType> & S, BinaryFunction & op, ObType e)
{
 //... important define BinaryFunction as taking const args !
typename std::set<ObType>::const_iterator beg(S.cbegin()), offend(S.cend());
bool isGroup = true;
for (std::set<ObType>::const_iterator ia = beg; ia != offend && noProblems; ++ia)
{

        // ---------- (3) --------------
        if( op( *ia, e)) != *ia  || op( e, *ia)) != *ia) 
            isGroup = false;
        // -----------------------------
Run Code Online (Sandbox Code Playgroud)

一般来说,这并不直接指示单位元素。带有我们众所周知的 + 的整数或其他算术类型的简单示例只是最简单且不可扩展的示例之一,即在环 Z 的分数域中,+ 的 Q ( Z) , e由一对 [0, 1] 和“ x ”为 [1,1]。因此,为了使其更通用,您必须迭代元素,选择e并调用以检查 (3) 是否适用于所有元素。op

template <typename ObType, typename BinaryFunction>
bool isGroup( const std::set<ObType> & S, BinaryFunction & op)
{
     //... important define BinaryFunction as taking const args !
    typename std::set<ObType>::const_iterator beg(S.cbegin()), offend(S.cend());

    for (std::set<ObType>::const_iterator ia = beg; ia != offend; ++ia)
    {

        // ---------- (3) -------------
        /* let e be an *ia */
        ObType e = *ia; 
        bool isGroup = true;
        for ( auto ia2 : S) {

            if( op( ia2, e)) != ia2  || op( e, ia2)) != ia2) {
                isGroup = false;
                break;
            }

            // identity found, set e_ in class to e and return
            if( isGroup) {
                e_ = e;
               return true;
            }
        }
    }

    /* identity not found, this is not a group */
    return false;
}
Run Code Online (Sandbox Code Playgroud)