/* Copyright © 2025 filifa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package cmd import ( "fmt" "math/big" "strconv" "github.com/spf13/cobra" "scm.dairydemon.net/filifa/mathtools/internal/lib" ) var firstKind bool var secondKind bool var stirlingTop string var stirlingBottom string var stirlingUnsigned bool func stirling(cmd *cobra.Command, args []string) { n, err := strconv.Atoi(stirlingTop) if err != nil { cobra.CheckErr("invalid input " + stirlingTop) } k, err := strconv.Atoi(stirlingBottom) if err != nil { cobra.CheckErr("invalid input " + stirlingBottom) } var result *big.Int if firstKind { result = lib.Stirling1(n, k) } else if secondKind { result = lib.Stirling2(n, k) } if stirlingUnsigned { result.Abs(result) } fmt.Println(result) } // stirlingCmd represents the stirling command var stirlingCmd = &cobra.Command{ Use: "stirling [-1|-2] -n N -k N", Short: "Compute the Stirling numbers", Long: `Compute the Stirling numbers. The Stirling numbers of the first kind give the coefficients in the expansion of the falling factorial. For example, x(x-1)(x-2) = x^3 - 3x^2 + 2x Consequently, s(3,1) = 2, s(3,2) = -3, and s(3,3) = 3. The Stirling numbers of the second kind count the number of ways to partition a set of n objects into k non-empty subsets. For example, there are 7 ways to partition a set of 4 objects into 2 non-empty subsets: {1} {2,3,4} {2} {1,3,4} {3} {1,2,4} {4} {1,2,3} {1,2} {3,4} {1,3} {2,4} {1,4} {2,3} Therefore S(4,2) = 7.`, Run: stirling, } func init() { rootCmd.AddCommand(stirlingCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // stirlingCmd.PersistentFlags().String("foo", "", "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // stirlingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") stirlingCmd.Flags().BoolVarP(&firstKind, "first", "1", false, "Compute Stirling numbers of the first kind") stirlingCmd.Flags().BoolVarP(&secondKind, "second", "2", false, "Compute Stirling numbers of the second kind") stirlingCmd.MarkFlagsMutuallyExclusive("first", "second") stirlingCmd.MarkFlagsOneRequired("first", "second") stirlingCmd.Flags().StringVarP(&stirlingTop, "n", "n", "", "n") stirlingCmd.Flags().StringVarP(&stirlingBottom, "k", "k", "", "k") stirlingCmd.MarkFlagRequired("n") stirlingCmd.MarkFlagRequired("k") stirlingCmd.Flags().BoolVar(&stirlingUnsigned, "unsigned", false, "output the absolute value of the number (only relevant for first kind)") }