factory.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package ops
  2. import (
  3. "makarna/pkg/backend/cpu"
  4. "makarna/pkg/tensor"
  5. )
  6. // Zeros creates a new zero-filled tensor
  7. func Zeros(shape tensor.Shape) *cpu.Tensor {
  8. return cpu.NewTensor(shape, nil) // Go zeros by default
  9. }
  10. // Ones creates a new tensor filled with 1.0
  11. func Ones(shape tensor.Shape) *cpu.Tensor {
  12. data := make([]float32, shape.NumElements())
  13. for i := range data {
  14. data[i] = 1.0
  15. }
  16. return cpu.NewTensor(shape, data)
  17. }
  18. // Full creates a new tensor filled with a specific value
  19. func Full(shape tensor.Shape, value float32) *cpu.Tensor {
  20. data := make([]float32, shape.NumElements())
  21. for i := range data {
  22. data[i] = value
  23. }
  24. return cpu.NewTensor(shape, data)
  25. }
  26. // Arange creates a 1D tensor with values [start, start+step, start+2*step, ...)
  27. func Arange(start, end, step float32) *cpu.Tensor {
  28. n := int((end - start) / step)
  29. if n <= 0 {
  30. return cpu.NewTensor(tensor.Shape{0}, nil)
  31. }
  32. data := make([]float32, n)
  33. for i := 0; i < n; i++ {
  34. data[i] = start + float32(i)*step
  35. }
  36. return cpu.NewTensor(tensor.Shape{n}, data)
  37. }