C ++程序没有认识到{-1,0,1}是一个组

时间:2014-06-04 20:01:47

标签: c++

所以我有一个函数来查看给定集合和运算符是否形成一个组。组的定义如下:

         A set  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 op b) op c = a op (b op c)
        (3) There is an element 0 in S such that a op 0 = 0 for all a in S
        (4) If a in S, then there is a b in S such that a op b = b op a = 0

根据此定义,带有运算符{-1, 0, 1}的集*应该是具有标识元素0的组。

但是,我实施的群组检查功能并不起作用。我的代码

template <typename ObType, typename BinaryFunction>
bool isGroup(const std::set<ObType> & S, BinaryFunction & op, ObType iden)
{
    /*
       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 op b) op c = a op (b op c)
            (3) There is an element 0 in S such that a op 0 = 0 for all a in S
            (4) If a in S, then there is a b in S such that a op b = b op a = 0
    */
    typename std::set<ObType>::const_iterator beg(S.cbegin()), offend(S.cend());
    bool noProblemsYet(true), foundIdentity(false);
    for (typename std::set<ObType>::const_iterator ia = beg; ia != offend && noProblemsYet; ++ia)
    {
        bool isIdentity = true;
        for (typename std::set<ObType>::const_iterator ib = beg; ib != offend && noProblemsYet; ++ib)
        {
            // ---------- (1) --------------
            if (S.count(op(*ia, *ib)) == 0)
                noProblemsYet = false;
            // ---------- (3) --------------
            if (op(*ia, *ib) != op(*ib, *ia) || op(*ib, *ia) != *ib)
                isIdentity = false;
            // -----------------------------
            for (typename std::set<ObType>::const_iterator ic = beg; ic != offend && noProblemsYet; ++ic)
            {
                // ---------- (2) -------------
                if (op(op(*ia, *ib), *ic) != op(*ia, op(*ib, *ic)))
                    noProblemsYet = false;
                // ----------------------------
            }
        }
        if (isIdentity)
        {
            foundIdentity = true;
            iden = *ia;
        }
    }

    if (noProblemsYet)
    {
        if (!foundIdentity)
            noProblemsYet = false;
        for (typename std::set<ObType>::const_iterator ia = beg; ia != offend && noProblemsYet; ++ia)
        {
            bool foundInverse = false;
            for (typename std::set<ObType>::const_iterator ib = beg; ib != offend && noProblemsYet; ++ib)
            {
                if (op(*ia, *ib) == op(*ib, *ia) && op(*ia, *ib) == iden)
                {
                    foundInverse = true;
                    break;
                }
            }
            // ---------- (4) -------------
            if (!foundInverse)
                noProblemsYet = false;
            // ----------------------------
        }
    }

    return noProblemsYet;
}

template <typename T>
class Multiplier
{
    private:
        static const char symbol = '*';
    public:
        T operator() (const T & x, const T & y) const { return x * y; };
        char getSymbol(void) const { return symbol; };
};

template <typename T>
std::string set2Str(const std::set<T> & S)
{
    std::string retstr = "{";
    typename std::set<T>::const_iterator it(S.cbegin()), offend(S.cend());
    if (it != offend)
        retstr.append(std::to_string(*it++));
    while (it != offend)
        retstr.append("," + std::to_string(*it++));
    retstr.push_back('}');
    return retstr;
}

int main()
{
    std::set<int> S = { 0, 1, -1 };
    Multiplier<int> m;
    int i;
    std::cout << set2Str(S) << std::endl;
    std::cout << "with operator " << m.getSymbol() << std::endl;
    if (isGroup(S, m, i))
        std::cout << "is a group, with identity element " << i;
    else
        std::cout << "is not a group.";
        return 0;
}

出于某种原因输出

{-1,0,1}
is not a group.

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

您对某个群组的定义不正确。

该行

    (3) There is an element 0 in S such that a op 0 = 0 for all a in S

应该是

    (3) There is an element 0 in S such that a op 0 = a for all a in S

集合{-1,0,+ 1}在乘法运算下不会形成一个组。

数字+1确实充当此集合的标识元素,但数字0没有反转,这是

所需的
   (4) If a in S, then there is a b in S such that a op b = b op a = 0

集{-1,+ 1} 确实在乘法下形成一个组。