Wednesday, October 8, 2025

OCAML VECTORS & MATRICES FOR AI

 

REVISED: Wednesday, October 8, 2025                                        





 1. OCAML VECTORS & MATRICES FOR AI

(*
============================
ocaml C:\AI2025\lesson3.ml

Lesson 3: Vectors & Matrices for AI
============================
*)
(*
Objective: 
*)

(* -----------------------------
   1. Vector and matrix types
   ----------------------------- *)
type vector = float list
type matrix = float list list

(* -----------------------------
   2. Vector operations
   ----------------------------- *)

(* Vector addition *)
let rec vector_add v1 v2 =
  match v1, v2 with
  | [], [] -> []
  | x1::xs1, x2::xs2 -> (x1 +. x2) :: vector_add xs1 xs2
  | _ -> failwith "Vectors must have the same length"

(* Scalar multiplication *)
let rec scalar_mul scalar v =
  match v with
  | [] -> []
  | x::xs -> (scalar *. x) :: scalar_mul scalar xs

let rec dot_product v1 v2 =
  match v1, v2 with
  | [], [] -> 0.
  | x1::xs1, x2::xs2 -> (x1 *. x2) +. dot_product xs1 xs2
  | _ -> failwith "Vectors must have the same length"

(* -----------------------------
   3. Linear transformation (matrix * vector)
   ----------------------------- *)
let matrix_vector_mul m v =
  List.map (fun row -> dot_product row v) m

(* -----------------------------
   4. Gradient computation (for f(x) = w·x + b)
   ----------------------------- *)
(* w: weight vector, x: input vector, b: bias *)
let linear_forward w x b =
  dot_product w x +. b

(* Derivative with respect to weights (gradient) *)
let gradient_w x _y = x  (* For f(x) = w·x + b, df/dw = x *)

(* Derivative with respect to bias *)
let gradient_b _x _y = 1.  (* df/db = 1 *)

(* -----------------------------
   5. Example usage
   ----------------------------- *)

(* Define a weight vector, input vector, and bias *)
let w = [0.5; -1.2; 0.3]
let x = [1.0; 2.0; 3.0]
let b = 0.1

(* Forward pass *)
let y_pred = linear_forward w x b
let () = Printf.printf "Linear forward output y_pred = %f\n" y_pred

(* Compute gradients *)
let grad_w = gradient_w x y_pred
let grad_b = gradient_b x y_pred
let () = Printf.printf "Gradient w = [%s]\n"
  (String.concat "; " (List.map string_of_float grad_w))
let () = Printf.printf "Gradient b = %f\n" grad_b

(* -----------------------------
   6. Batch matrix-vector multiplication (multiple inputs)
   ----------------------------- *)
let inputs = [
  [1.; 0.; 2.];
  [0.; 1.; 1.];
  [3.; 2.; 0.]
]

(* 
Correction Explanation:
- The original code used the (>>) operator to compose functions.
- OCaml’s standard library does not define (>>), so the compiler reported:
  “Unbound value (>>).”
- To fix this, we replaced the use of (>>) with explicit function calls and
  let-bindings for clarity and full compatibility with standard OCaml.
*)

(* Forward pass for batch (fixed) *)
let batch_output =
  List.map
    (fun input ->
       (* Apply matrix_vector_mul first to compute a 1-element list of dot products *)
       let result_list = matrix_vector_mul [w] input in
       (* Take the first (and only) element of the result *)
       let y = List.hd result_list in
       (* Add the bias term *)
       y +. b)
    inputs

let () =
  Printf.printf "Batch outputs = [%s]\n"
    (String.concat "; " (List.map string_of_float batch_output))

2. CONCLUSION

Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

OCaml version: The OCaml toplevel, version 5.3.0
Coq-LSP version: 0.2.3
Loading personal and system profiles took 1084ms.
PS C:\Users\User> ocaml C:\AI2025\lesson3.ml
Linear forward output y_pred = -0.900000
Gradient w = [1.; 2.; 3.]
Gradient b = 1.000000
Batch outputs = [1.2; -0.8; -0.8]
PS C:\Users\User>

3. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.