Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
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 ¶
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 ¶
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 ¶
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
Types ¶
type Set ¶
Set is the set for mathematical set operations. The slice []E must be sorted for correct calculation.
func Difference ¶
Difference returns the result of A−B. For empty set ∅ and non-empty set A,B, result will be
- ∅−∅ : ∅
- ∅−B : ∅
- A−∅ : A
func Intersection ¶
Intersection returns the result of A∩B. For empty set ∅ and non-empty set A,B, result will be
- ∅∩∅ : ∅
- ∅∩B : ∅
- A∩∅ : ∅
func NewSet ¶
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 ¶
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]) 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]