<commit_before>(defun parse-string-to-float (line)
  (with-input-from-string (s line)
    (loop
       :for num := (read s nil nil)
       :while num
       :collect num)))

(defun get-num-out (str)
  (declare (inline parse-string-to-float))
  (do* ((pos (position #\  str) (position #\  (subseq str (+ 1 poss))))
        (strr (subseq str 0 pos) (subseq str (1+ poss) (if (eql pos nil) nil (+ 1 poss pos))))
        (num (car (parse-string-to-float strr)) (car (parse-string-to-float strr)))
        (nums (list num) (append nums (list num)))
        (poss pos (if (eql pos nil) nil (+ 1 poss pos))))
       ((eql pos nil)
        nums)))

(defun *list-to-array (ll)
  (let* ((len (length ll))
         (ar (make-array len :initial-contents ll)))
    ar))

(defun read-data ()
  (with-open-file (f #P"./testData.txt"
                     :direction :input)
    (declare (inline *list-to-array))
    (do* ((l (read-line f) (read-line f nil 'eof))
          (x (get-num-out l) (if (eql l 'eof) '(0) (get-num-out l)))
          (ar (*list-to-array x) (*list-to-array x))
          (pp (print ar) (print ar)))
        ((eql l 'eof) "Finsh read data"))))

(defun array-slice (m i)
  "only work for two dimensions matrix. i is index number"
  (let* ((dim (array-dimensions m))
         (colNum (cadr dim)))
    (return-from array-slice (make-array colNum :initial-contents 
                        (loop for id from 0 to (1- colNum) collect
                             (aref m i id))))))

(declaim (inline array-slice))

(defun array-multiply (array1 array2)
  (return-from array-multiply
    (loop for i across array1
       for ii across array2 sum
         (* i ii))))

(declaim (inline array-multiply))

(defun compute-cost (X theta y)
  "X is matrix. y is result."
  (let ((result)
        (rowNum (1- (elt (array-dimensions X) 0))))
    (setf result 
    (loop for r from 0 to rowNum for temp = (array-slice X r) sum
         (expt
          (- (array-multiply temp theta) (elt y r)) 2)))
    result))

(defun partial-derivative (X theta y)
  "X is a r*c matrix"
  (let ((result)
        (colNum (1- (elt (array-dimensions X) 1)))
        (rowNum (1- (elt (array-dimensions X) 0))))
    (setf result 
          (loop for c from 0 to colNum collect
               (/
               (loop for r from 0 to rowNum for temp = (array-slice X r) sum
                    (* (- (array-multiply temp theta) (elt y r))
                       (elt temp c)))
               (1+ rowNum))))
    result))<commit_msg>start to copy the exercise<commit_after>(defun parse-string-to-float (line)
  (with-input-from-string (s line)
    (loop
       :for num := (read s nil nil)
       :while num
       :collect num)))

(defun get-num-out (str)
  (declare (inline parse-string-to-float))
  (do* ((pos (position #\  str) (position #\  (subseq str (+ 1 poss))))
        (strr (subseq str 0 pos) (subseq str (1+ poss) (if (eql pos nil) nil (+ 1 poss pos))))
        (num (car (parse-string-to-float strr)) (car (parse-string-to-float strr)))
        (nums (list num) (append nums (list num)))
        (poss pos (if (eql pos nil) nil (+ 1 poss pos))))
       ((eql pos nil)
        nums)))

(defun *list-to-array (ll)
  (let* ((len (length ll))
         (ar (make-array len :initial-contents ll)))
    ar))

(defun read-data ()
  (let ((result))
    (with-open-file (f #P"./testData.txt"
                       :direction :input)
      (declare (inline *list-to-array))
      (do* ((l (read-line f) (read-line f nil))
            (x (get-num-out l) (if (not l) nil (get-num-out l)))
            (ar (*list-to-array x) (*list-to-array x))
            (p (push ar result) (push ar result)))
           ((not l) (print "Finsh read data" ))))
    (cdr result)))

(defun array-slice (m i)
  "only work for two dimensions matrix. i is index number"
  (let* ((dim (array-dimensions m))
         (colNum (cadr dim)))
    (return-from array-slice (make-array colNum :initial-contents 
                        (loop for id from 0 to (1- colNum) collect
                             (aref m i id))))))

(declaim (inline array-slice))

(defun array-multiply (array1 array2)
  (return-from array-multiply
    (loop for i across array1
       for ii across array2 sum
         (* i ii))))

(declaim (inline array-multiply))

(defun compute-cost (X theta y)
  "X is matrix. y is result."
  (let ((result)
        (rowNum (1- (elt (array-dimensions X) 0))))
    (setf result 
    (loop for r from 0 to rowNum for temp = (array-slice X r) sum
         (expt
          (- (array-multiply temp theta) (elt y r)) 2)))
    result))

(defun partial-derivative (X theta y)
  "X is a r*c matrix"
  (let ((result)
        (colNum (1- (elt (array-dimensions X) 1)))
        (rowNum (1- (elt (array-dimensions X) 0))))
    (setf result 
          (loop for c from 0 to colNum collect
               (/
               (loop for r from 0 to rowNum for temp = (array-slice X r) sum
                    (* (- (array-multiply temp theta) (elt y r))
                       (elt temp c)))
               (1+ rowNum))))
    result))

;;; exercise below
;;;
;;;

(defvar *X*)
(setf *X*
      (let ((temp
             (loop for ar in (read-data) collect
                  (list 1 (elt ar 0)))))
        (make-array (list (length temp) (length (car temp))) :initial-contents temp)))

(defvar *y*)
(setf *y*
      (loop for ar in (read-data) collect
           (elt ar 1)))
{'fraction_non_alphanumeric': 0.1545000924043615, 'fraction_numerical': 0.008870818702642764, 'mean_word_length': 2.0302351623740202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '76026', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1825, 'n_words': 646}