mathtools/cmd/stirling.go

93 lines
2.6 KiB
Go
Raw Normal View History

2025-08-28 02:37:45 +00:00
/*
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 <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"math/big"
"strconv"
2025-08-28 02:37:45 +00:00
"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 {
2025-08-28 02:37:45 +00:00
cobra.CheckErr("invalid input " + stirlingTop)
}
k, err := strconv.Atoi(stirlingBottom)
if err != nil {
2025-08-28 02:37:45 +00:00
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",
Short: "Compute the Stirling numbers",
Long: `Compute the Stirling numbers.`,
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)")
}