zmath

package
v0.0.0-alpha.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[E cmp.Ordered](a, b Set[E]) bool

Equal returns the result of A=B. In other words, A⊆B and B⊆A. For empty set ∅ and non-empty set A,B, result will be

  • ∅=∅ : true
  • ∅=B : false
  • A=∅ : false
Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	var a, b zmath.Set[int]

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5, 6}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 3, 5})
	fmt.Printf("%v=%v %v\n", a, b, zmath.Equal(a, b))

}
Output:

[]=[] true
[0 2 5]=[] false
[]=[0 2 5] false
[0 2 5]=[0 2 5] true
[0 2 5 6]=[0 2 5] false
[0 2 5]=[0 2 3 5] false

func ProperSubset

func ProperSubset[E cmp.Ordered](a, b Set[E]) bool

ProperSubset returns the result of A⊂B. ProperSubset is the inverse operation of the ProperSuperset. For empty set ∅ and non-empty set A,B, result will be

  • ∅⊂∅ : false
  • ∅⊂B : true
  • A⊂∅ : false
Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	var a, b zmath.Set[int]

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5, 6}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 3, 5})
	fmt.Printf("%v⊂%v %v\n", a, b, zmath.ProperSubset(a, b))

}
Output:

[]⊂[] false
[0 2 5]⊂[] false
[]⊂[0 2 5] true
[0 2 5]⊂[0 2 5] false
[0 2 5 6]⊂[0 2 5] false
[0 2 5]⊂[0 2 3 5] true

func ProperSuperset

func ProperSuperset[E cmp.Ordered](a, b Set[E]) bool

ProperSuperset returns the result of A⊃B. ProperSuperset is the inverse operation of the ProperSubset. For empty set ∅ and non-empty set A,B, result will be

  • ∅⊃∅ : false
  • ∅⊃B : false
  • A⊃∅ : true

func Subset

func Subset[E cmp.Ordered](a, b Set[E]) bool

Subset returns the result of A⊆B. Subset is the inverse operation of the Superset. For empty set ∅ and non-empty set A,B, result will be

  • ∅⊆∅ : true
  • ∅⊆B : true
  • A⊆∅ : false
Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	var a, b zmath.Set[int]

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

	a, b = zmath.NewSet([]int{}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5, 6}), zmath.NewSet([]int{0, 2, 5})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

	a, b = zmath.NewSet([]int{0, 2, 5}), zmath.NewSet([]int{0, 2, 3, 5})
	fmt.Printf("%v⊆%v %v\n", a, b, zmath.Subset(a, b))

}
Output:

[]⊆[] true
[0 2 5]⊆[] false
[]⊆[0 2 5] true
[0 2 5]⊆[0 2 5] true
[0 2 5 6]⊆[0 2 5] false
[0 2 5]⊆[0 2 3 5] true

func Superset

func Superset[E cmp.Ordered](a, b Set[E]) bool

Superset returns the result of A⊇B. Superset is the inverse operation of the Subset. For empty set ∅ and non-empty set A,B, result will be

  • ∅⊇∅ : true
  • ∅⊇B : false
  • A⊇∅ : true

Types

type Set

type Set[E cmp.Ordered] []E

Set is the set for mathematical set operations. The slice []E must be sorted for correct calculation.

func Difference

func Difference[E cmp.Ordered](a, b Set[E]) Set[E]

Difference returns the result of A−B. For empty set ∅ and non-empty set A,B, result will be

  • ∅−∅ : ∅
  • ∅−B : ∅
  • A−∅ : A

func Intersection

func Intersection[E cmp.Ordered](a, b Set[E]) Set[E]

Intersection returns the result of A∩B. For empty set ∅ and non-empty set A,B, result will be

  • ∅∩∅ : ∅
  • ∅∩B : ∅
  • A∩∅ : ∅

func NewSet

func NewSet[S ~[]E, E cmp.Ordered](x S) Set[E]

NewSet returns a new sorted set from the given slice x. It modifies the given slice of x. Callers should not modify the slice x after called NewSet.

func Union

func Union[E cmp.Ordered](a, b Set[E]) Set[E]

Union returns the result of A∪B. For empty set ∅ and non-empty set A,B, result will be

  • ∅∪∅ : ∅
  • ∅∪B : B
  • A∪∅ : A

func (*Set[E]) Add

func (s *Set[E]) Add(elem E)

Add adds elem to the set.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	s := zmath.NewSet([]int{0, 2, 5, 8, 10})
	s.Add(1)
	s.Add(5)
	s.Add(9)
	fmt.Println(s)
}
Output:

[0 1 2 5 5 8 9 10]

func (*Set[E]) AddElems

func (s *Set[E]) AddElems(elems ...E)

AddElems adds multiple elements to the set.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	s := zmath.NewSet([]int{0, 2, 5, 8, 10})
	s.AddElems([]int{-1, 3, 8, 12}...)
	fmt.Println(s)
}
Output:

[-1 0 2 3 5 8 8 10 12]

func (*Set[E]) Has

func (s *Set[E]) Has(elem E) bool

Has returns if the set s has at least one elem.

func (*Set[E]) Remove

func (s *Set[E]) Remove(elem E)

Remove removes the given elem from the set.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	s := zmath.NewSet([]int{0, 2, 5, 8, 10})
	s.Remove(1)
	s.Remove(5)
	fmt.Println(s)
}
Output:

[0 2 8 10]

func (*Set[E]) RemoveElems

func (s *Set[E]) RemoveElems(elems ...E)

RemoveElems removes multiple elements from the set.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zmath"
)

func main() {
	s := zmath.NewSet([]int{0, 2, 5, 8, 10})
	s.RemoveElems([]int{-1, 5, 8, 12}...)
	fmt.Println(s)
}
Output:

[0 2 10]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL