File indexing completed on 2024-05-12 03:41:58

0001 /*************************************************************************************
0002  *  Copyright (C) 2022 Aleix Pol Gonzalez <aleixpol@kde.org>                         *
0003  *  Copyright (C) 2022 Stephen Swanson <stephen.swanson@mailbox.org>                 *
0004  *                                                                                   *
0005  *  This program is free software; you can redistribute it and/or                    *
0006  *  modify it under the terms of the GNU General Public License                      *
0007  *  as published by the Free Software Foundation; either version 2                   *
0008  *  of the License, or (at your option) any later version.                           *
0009  *                                                                                   *
0010  *  This program is distributed in the hope that it will be useful,                  *
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0013  *  GNU General Public License for more details.                                     *
0014  *                                                                                   *
0015  *  You should have received a copy of the GNU General Public License                *
0016  *  along with this program; if not, write to the Free Software                      *
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0018  *************************************************************************************/
0019 
0020 #include "combinatronics.h"
0021 
0022 #include <QCoreApplication>
0023 
0024 #include "expression.h"
0025 #include "list.h"
0026 #include "value.h"
0027 
0028 using namespace Analitza;
0029 
0030 const QString CombinationCommand::id = QStringLiteral("comb");
0031 const ExpressionType CombinationCommand::type = ExpressionType(ExpressionType::Lambda)
0032                                         .addParameter(ExpressionType::Value)
0033                                         .addParameter(ExpressionType::Value)
0034                                         .addParameter(ExpressionType::Value);
0035 
0036 Expression CombinationCommand::operator()(const QList<Expression>& args)
0037 {
0038     const auto a = args.constFirst().toReal().intValue();
0039     const auto b = args.constLast().toReal().intValue();
0040 
0041     // a=n b=r
0042     uint n_factorial=1;
0043     for(int i=a; i>1.; i--) {
0044         n_factorial*=floor(i);
0045     }
0046     uint r_factorial=1;
0047     for(int i=b; i>1.; i--) {
0048         r_factorial*=floor(i);
0049     }
0050     uint rn = a - b;
0051     uint rn_factorial=1;
0052     for(int i=rn; i>1.; i--) {
0053         rn_factorial*=floor(i);
0054     }
0055     uint res = n_factorial / (r_factorial * rn_factorial);
0056     return Expression(Cn(res));
0057 }
0058 
0059 const QString PermutationCommand::id = QStringLiteral("perm");
0060 const ExpressionType PermutationCommand::type = ExpressionType(ExpressionType::Lambda)
0061                                         .addParameter(ExpressionType::Value)
0062                                         .addParameter(ExpressionType::Value)
0063                                         .addParameter(ExpressionType::Value);
0064 
0065 Expression PermutationCommand::operator()(const QList<Expression>& args)
0066 {
0067     const auto a = args.constFirst().toReal().value();
0068     const auto b = args.constLast().toReal().intValue();
0069 
0070     // a=n b=r
0071     if (b > a) {
0072         return Expression(Cn(0));
0073     } else {
0074         uint n_factorial=1;
0075         for(int i=a; i>1.; i--) {
0076             n_factorial*=floor(i);
0077         }
0078         uint rn = a - b;
0079         uint rn_factorial=1;
0080         for(int i=rn; i>1.; i--) {
0081             rn_factorial*=floor(i);
0082         }
0083         uint res = n_factorial / rn_factorial;
0084         return Expression(Cn(res));
0085     }
0086 }